http://www.cplusplus.com/reference/cstdlib/rand/
http://www.cplusplus.com/reference/cstdlib/rand/
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
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.
http://stackoverflow.com/questions/18392522/generating-uniformly-distributed-pseudo-random-numbers-in-c