[gameprogrammer] Re: Random Integer Problem :(
- From: Matthew Weigel <unique@xxxxxxxxxxx>
- To: gameprogrammer@xxxxxxxxxxxxx
- Date: Sun, 24 Sep 2006 12:32:09 -0500
Andrew Falgout wrote:
> Perhaps you can explain why the modulus would be the improper way of
> getting a random number?
Because it isn't a random number with infinite range.
If you look at RAND_MAX modulo an integer X (the range you want, 6 or 10
or whatever) it's very unlikely to be zero. If the modulus is non-zero,
and the random number generator is truly random, then the result you get
is subtly weighted to the lower numbers.
If RAND_MAX were 10 and the range you wanted was 6, then the range is
0-9 % 6 = (0, 1, 2, 3, 4, 5, 0, 1, 2, 3)
As you can see, a third of the numbers (4 and 5) are half as likely to
be the result as the smaller numbers are.
For sufficiently large RAND_MAX this doesn't much matter to most people,
although cryptographers have to worry about statistical-based attacks.
For games... it really shouldn't matter.
--
Matthew Weigel
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
- References:
- [gameprogrammer] Random Integer Problem :(
- From: Sahan Chandrasekara
- [gameprogrammer] Re: Random Integer Problem :(
- From: Chris Nystrom
- [gameprogrammer] Re: Random Integer Problem :(
- From: Olivier Delannoy
- [gameprogrammer] Re: Random Integer Problem :(
- From: Andrew Falgout
Other related posts:
- » [gameprogrammer] Random Integer Problem :(
- » [gameprogrammer] Re: Random Integer Problem :(
- » [gameprogrammer] Re: Random Integer Problem :(
- » [gameprogrammer] Re: Random Integer Problem :(
- » [gameprogrammer] Re: Random Integer Problem :(
- » [gameprogrammer] Re: Random Integer Problem :(
- » [gameprogrammer] Re: Random Integer Problem :(
- » [gameprogrammer] Re: Random Integer Problem :(
- » [gameprogrammer] Re: Random Integer Problem :(
- » [gameprogrammer] Re: Random Integer Problem :(
- » [gameprogrammer] Re: Random Integer Problem :(
- » [gameprogrammer] Re: Random Integer Problem :(
- [gameprogrammer] Random Integer Problem :(
- From: Sahan Chandrasekara
- [gameprogrammer] Re: Random Integer Problem :(
- From: Chris Nystrom
- [gameprogrammer] Re: Random Integer Problem :(
- From: Olivier Delannoy
- [gameprogrammer] Re: Random Integer Problem :(
- From: Andrew Falgout