2

In cplusplus.com reference it's stated that, using modulo operator when trying to generate random numbers will make lower numbers more likely:

random_var = rand() % 100 + 1; //this will generate numbers between 1-100

Why are lower numbers more likely? And if they're, why aren't we using this code below :

random_var = rand()/(RAND_MAX/100) + 1; //also will generate those, more uniform I guess
baris_ercan
  • 152
  • 2
  • 16

1 Answers1

5

Let's say RAND_MAX is 150. (Obviously it's not actually.) And we want numbers from 0-99. Then we do rand() % 100. Cool.

The problem is, what if RAND() returns a number greater than 100? Let's take 102. 102 % 100 = 2, and 2 % 100 = 2. So there is a 2/150 chance that we will get a 2 with the given algorithm. But a number above 50? There's only a 1/150 chance that we'll get it. The higher RAND_MAX, the less of a problem this is, but it remains an issue.

Notice that if RAND_MAX were divisible by the number that you wanted to "modulate" it by, all numbers would be equally likely. i.e if RAND_MAX were 200 rather than 150. Hope this helps!

Edit: the actual math.

RAND_MAX is guaranteed to be at least 32767. If we want a range from 0-99, we can do RAND() % 100. Then, numbers between 0 and 67 will all appear 328 possible times, while 68-99 will appear only 327 times each. That's a 1.0010071% chance for the first 68 numbers, and only a 0.9979553% chance for the rest of them. We want them all to be 1%! Usually not a major issue, but depending on the use case, could show some strange behavior.

vroomfondel
  • 3,056
  • 1
  • 21
  • 32
  • Thanks, I got it now, what if we used a float division for rand()/(RAND_MAX/100) in the second case. Would that solve the problem? – baris_ercan Aug 23 '13 at 00:02
  • I haven't run a simulation or done out the math, but I'm pretty sure that second method is correct. I think it might have some somewhat funky behavior due to the nature of floats, and will be a little slower, but It looks like it will be a flatter distribution. If I'm wrong, someone correct me! – vroomfondel Aug 23 '13 at 00:06
  • (You would want a floor of the float division, obviously, in the second method.) – vroomfondel Aug 23 '13 at 00:13