Author: Dann Corbit
Date: 18:00:35 06/07/04
Go up one level in this thread
On June 07, 2004 at 20:42:22, Dieter Buerssner wrote:
>On June 07, 2004 at 20:28:51, Dann Corbit wrote:
>
>>On June 07, 2004 at 20:19:12, Dann Corbit wrote:
>>
>>>ftp://cap.connx.com/pub/chess-engines/new-approach/frcpos.c
>>
>>It would be a lot better as a C++ class with srand((unsigned)time(NULL)) in the
>>constructor. Also, the C/C++ prng of questionable quality would be much better
>>replaced by the Mersenne Twister. In fact, the MT prng is so good, you could
>>replace the monkey business with floating point and division by a modulus
>>operator.
>
>For this purpose - yes. In general both approaches are flawed, because not all
>positions will be returned with exactly the same probability. RAND_MAX%960 != 0.
>I know, that your method is the method advertized at many places. I don't like
>it too much. It is possibly dangerous for some simulations. A better method is,
>to find the largest value x <= RAND_MAX, for which x % 960 == 0. Then skip all
>returns of rand() > x. Like in the following function:
If you make a 64 bit MT generator, and do a modulus, the result will be
indistinguisable from perfect as far as measurement is concerned (even after a
quadrillion runs). It does not matter that 960 is not prime.
With a bad PRNG, it is unlikely but conceivable that you could be stuck in the
loop below for quite a while.
>unsigned rand_range(unsigned range)
>{
> unsigned long rmax, r, d;
> /* find the largest number rmax <= MY_RAND_MAX, for which
> (rmax+1) % range == 0.
> All returns from rand() > rmax will be skipped, to guarantee
> equal probability for all return values. */
> d = (MY_RAND_MAX+1U-range) / range + 1;
> rmax = d * range - 1; /* -1 to avoid "overflow to zero" */
> do
> r = PRNG();
> while (r > rmax);
> return r/d;
>}
>
>
>Which guarantees equal probability for all values.
>
>From your code:
>
> index = (size_t) ((double) rand() / ((double) RAND_MAX + 1) * 980);
>
>The 980 does not look right.
>
>Regards,
>Dieter
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.