Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: random book moves/ random generator

Author: David Blackman

Date: 23:27:47 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
>

Bob's suggestion was a likely one, but it sounds now like that wasn't your
problem.

The other possibility is that a lot of library random number generators aren't
very good. They are especially bad if you use

rand() % total

to get a smaller number. Somehow the low order bits of the number aren't very
random. Usually the high order bits are better. One way to get at the high order
bits is

number = (int) ( (double)(rand()) / DMAXRAND * total ) ;

where DMAXRAND is a double, one bigger than the biggest possible return value
from rand().

Roundoff error is a possible problem here. If you are paranoid you will check
the result is in the range 0 to total-1 .

Another possibility is to build your own random number generator so you are not
at the mercy of the clueless people who often write rand() functions for C
programming libraries. But to do that well, you really have to know your stuff.

Yet another possibility is to use the rand48 series of functions that come with
some libraries. If it is available on all your platforms, it is worth a look.
Most implementations are very good.



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.