Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: A question about random numbers

Author: Scott Gasch

Date: 16:38:22 07/22/04

Go up one level in this thread


On July 22, 2004 at 16:51:03, Steven Edwards wrote:

>On July 22, 2004 at 16:45:29, Antonio Senatore wrote:
>
>>
>>Hi friends:
>>
>>I need to generate a random number between 0 and 100000. I'm using the following
>>formula:
>>
>>srand((unsigned)(double)time(NULL));
>>random_number = (unsigned int) ((double) rand() / (double) RAND_MAX  * 100000);
>>
>>However, the numbers that I obtain don't seem to be very aleatory that say;
>>please, could someone tell me what am I doing bad?
>
>Don't use srand() or rand().
>
>Use random() and srandom() instead.

Or if you really need randomness then:

1. Read Knuth vol 2.  There is an entire chapter devoted to random number
generators and different tests of randomness.

2. Read Numerical Recipes (in bla, I have bla==C but they make everything from
FORTRAN to C++ etc...).  There are again several alternative implementations of
good random number generators.

3. Consider using a better source of randomness.  On Windows, look at
CryptGenRandom.  On freebsd look at reading /dev/random.  I'm sure Linux has one
too.  The Windows/FreeBSD ones distill randomness from stuff like process
termination times, time between interrupts on the system, etc...

4. For seeding my hash signature seeds in my chess program I used the mersenne
twister code from Crafty (after getting permission from the original authors
whose email addresses are in the code).

For a project I was working on a few years ago I had to measure the randomness
of a sequence of numbers.  Let me be the first to tell you that srand() from
most C runtime libraries is _terrible_.  Steer clear for anything important.

Scott



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.