[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Bug-gnubg] Dice Roll Generation
From: |
Joern Thyssen |
Subject: |
Re: [Bug-gnubg] Dice Roll Generation |
Date: |
Fri, 15 Aug 2003 15:05:32 +0000 |
User-agent: |
Mutt/1.4.1i |
On Thu, Aug 14, 2003 at 02:52:00PM -0600, Michael Petch wrote
>
> I was reviewing the dice generation algorithms on the weekend in more
> detail than I had before, and I was curious about something.
>
> I noticed that most of the dice generation is based on finding the
> remainder of integer division (dividing a value by 6 and take
> remainder).
>
> What I find odd about this is that relative lengths have been taken to
> ensure the initial seeds are sufficiently random however using modulus
> to define a range introduces a slight bias for certain remainders.
I've modified the code to use:
anDice[ 0 ] = 1+(int) (6.0*rand()/(RAND_MAX+1.0));
instead. This is suggested on the rand(3) man page:
In Numerical Recipes in C: The Art of Scientific Computing
(William H. Press, Brian P. Flannery, Saul A. Teukolsky,
William T. Vetterling; New York: Cambridge University
Press, 1992 (2nd ed., p. 277)), the following comments are
made:
"If you want to generate a random integer between 1
and 10, you should always do it by using high-order
bits, as in
j=1+(int) (10.0*rand()/(RAND_MAX+1.0));
and never by anything resembling
j=1+(rand() % 10);
(which uses lower-order bits)."
Jørn
pgpvSK2vK9HH6.pgp
Description: PGP signature