The simplest way to get uniformly distributed results from rand
is something like this:
int limited_rand(int limit)
{
int r, d = RAND_MAX / limit;
limit *= d;
do { r = rand(); } while (r >= limit);
return r / d;
}
The result will be in the range 0
to limit-1
, and each will occur with equal probability as long as the values 0
through RAND_MAX
all had equal probability with the original rand
function.
Other methods such as modular arithmetic or dividing without the loop I used introduce bias. Methods that go through floating point intermediates do not avoid this problem. Getting good random floating point numbers from rand
is at least as difficult. Using my function for integers (or an improvement of it) is a good place to start if you want random floats.
Edit: Here's an explanation of what I mean by bias. Suppose RAND_MAX
is 7 and limit
is 5. Suppose (if this is a good rand
function) that the outputs 0, 1, 2, ..., 7 are all equally likely. Taking rand()%5
would map 0, 1, 2, 3, and 4 to themselves, but map 5, 6, and 7 to 0, 1, and 2. This means the values 0, 1, and 2 are twice as likely to pop up as the values 3 and 4. A similar phenomenon happens if you try to rescale and divide, for instance using rand()*(double)limit/(RAND_MAX+1)
Here, 0 and 1 map to 0, 2 and 3 map to 1, 4 maps to 2, 5 and 6 map to 3, and 7 maps to 4.
These effects are somewhat mitigated by the magnitude of RAND_MAX
, but they can come back if limit
is large. By the way, as others have said, with linear congruence PRNGs (the typical implementation of rand
), the low bits tend to behave very badly, so using modular arithmetic when limit
is a power of 2 may avoid the bias problem I described (since limit
usually divides RAND_MAX+1
evenly in this case), but you run into a different problem in its place.