Mail Archives: djgpp/1997/10/09/20:05:20
At 04:20 10/7/1997 -0300, Antonio Dias wrote:
>Hi all,
>
>I was looking all that messages about random numbers and I decided to
>investigate. I discover that to generate a true random number you must
>use ramdom() function instead of rand().
This is wrong. rand() and random() both generate random numbers, in fact
they are the same code under DJGPP. rand() generates a number between 0 and
RAND_MAX and comes from ANSI (IIRC), and random() generates a number between
0 and MAXINT and comes from BSD. It so happens that RAND_MAX == MAXINT under
DJGPP.
>These functions need to be
>initialized with a seed to work well. This is acomplished using
>srandom() function. The srandom() function expects a int as seed and if
>your program always send the same seed it will *always* get the same
>ramdom numbers.
If you use rand(), use the srand() function to seed it. rand() is probably
preferable since it is ANSI standard and more portable.
> The problem now consists in pass a new seed to srandom
>everytime you start your program. I acomplish this using the hundreds of
>seconds from machine's internal clock. Look the code below:
>[snipped]
That works, but srand(time(0)) is probably just as effective and much simpler.
Nate Eldredge
eldredge AT ap DOT net
- Raw text -