Mail Archives: djgpp/1997/05/05/23:14:28
On getting random numbers :
If you do a histogram of rand() % 100 or random() % 100 you will
find that its not uniform. If you image that rand return a uniform
number from 0-31 and you % 10 you map
[ rand val ] -> (val % 10)
[0,10,20,30] -> 0
[1,11,21,31] -> 1
[2,12,22] -> 2
[3,13,23] -> 3
[4,14,24] -> 4
. . . .
[9,19,29] -> 9
Depending on what your doing it may be close enough not to matter.
Somthing like this gets rid of this problem.
int rnd(int lo,int hi)
{
int rng = hi - lo + 1;
return ( lo + rng * rand() / MAX_INT );
}
I only spent a couple days trying to find why a modeler I was
writing always favored low number 8^)
- Raw text -