Originally posted by wervdon
Something like this:
srand(system time);
// utility function to return a random # between two limits
int number_range(int min, int max)
{
if (min > max) return min;
return ((rand() %(max-min+1)) + min);
}
Something like this:
srand(system time);
// utility function to return a random # between two limits
int number_range(int min, int max)
{
if (min > max) return min;
return ((rand() %(max-min+1)) + min);
}
See here for an explanation of this effect with respect to Java:
The same principle applies to your example above.
In essence, you must keep the result of rand() as a double-precision and base your outcome on the normalized distribution between 0 and 1. If you don't you skew the distribution and make it less random.
Not that I am making any connection between your sketched implementation above and the hypothetical implementation in Civ II, but games programmers are games programmers and all tend to read the same stuff :-)))
Summary: Truncation of rand() using % operator makes the resultant distribution LESS random.
Query: Any Firaxis programmers lurking out there, please tell me you didn't use the mod operator in the RNG :-))
DRM
Comment