Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: random book moves/ random generator

Author: Robert Hyatt

Date: 21:49:28 01/13/00

Go up one level in this thread


On January 13, 2000 at 22:21:17, Vincent Diepeveen wrote:

>On January 13, 2000 at 13:46:48, Robert Hyatt wrote:
>
>>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.
>
>I'm using it as is in the examples on how to use it :)
>
>>> 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.
>
>No i initialize it with seed time(NULL) that is the time in seconds since
>computer clock.
>
>So it *does* produce each times different numbers.
>
>However if we make graphs then it is simply producing too much numbers
>that are in range of a certain move.


One point is that you _never_ seed a RNG with an even number.  You have a
50-50 chance of doing so.  An easy solution is to forget the seed, but each
time, before you call the thing, grab a random number from the low-order part
of the millisecond timer, and throw out that many numbers from the stream.



>
>>
>>> 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.