Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: random book moves/ random generator

Author: Robert Hyatt

Date: 10:46:48 01/13/00

Go up one level in this thread


On January 13, 2000 at 11:22:20, Vincent Diepeveen wrote:

>I'm really amazed about next bug Thorsten clearly found:
>
>This is how i do my initialization of random
>generator as it is in all examples:
>
>   srand((unsigned int)time(NULL));
>
>Now if we have for example 3 moves A,B,C
>with chances that a move gets played:
>    A = 30
>    B = 30
>    C = 30
>total = 90
>
>Then obviously i want to play each move the same number of times.
>
>The way in which i pick a move is:
>
>number = rand()%total;
>
>   A ==>  0..29
>   B ==> 30..59
>   C ==> 60..89
>
>Now if i get more or less random numbers then i would expect that
>'number' falls in 1/3 of the times between 0..29, at 1/3 of the times
>between 30..59 and 1/3 of the times at 60..89
>
>However if i start diep THEN THIS DOESN'T HAPPEN, as Thorsten
>already figured out himself.
>
>Sure it sometimes picks a different move, but it favours a certain move
>a lot more than other moves.
>
>This happens ONLY when i restart the program each time. I don't restart
>within a second of course. Srand doesn't get initialized at the same
>number each time.
>
>If i just type inside the program 'newgame' and see what it has done now,
>then this problem happens a lot less. It seems working then. Problem are
>there clearly the first few numbers that i get from rand after initializing
>srand with the time since 1970.
>
>Questions
>
> a) is this problem known?

Yes.  It is caused by your using the random number generator incorrectly.

> b) what causes it?

These random number generators are called "pseudo-random number generators"
for a good reason.  They are supposed to produce the same series of numbers
if they are started from scratch.  Otherwise you couldn't debug a program
that uses random numbers.


> c) how to fix it?

Two answers:  If you can alter the seed, do so.  Grab the current wall-clock
time in milliseconds and use that to seed the number.

If you can't do that, each time you call the RNG, grab the wall clock time,
toss everything but the millisecond/microsecond part (depending on which time
function you use) and then modulo that with 1000.  Call the RNG that many times,
_before_ you call it to get the real random number.

This is basic programming, BTW.  Re-read anything you have on using
pseudo-random number generators.  :)

BTW if this didn't work like this, how could you _ever_ use random numbers to
produce the hash signature?  No Book would work, for sure.  :)



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.