[computer-go] C++ random numbers

Phil G go at thinkedge.com
Tue Aug 8 16:35:38 PDT 2006


Be careful with rand(). rand() will return a value up to RAND_MAX, and on some systems that can be limited to 32767. The GNU library has it set to 2147483647, but that does not mean it is universal across all operating system platforms and compilers. Hopefully, it doesn't matter. I think 32767 is the minimum defined by the standard.
 
I remember reading that rand() isn't that random, especially the lower bits. It's just a "good" random number generator for general use. However, if you use it for a Zobrist hash key you might have more collisions than you wish.
 
For general use, I use this :
 
U64 rand64(void)
{
    return rand() ^ ((U64)rand() << 15) ^ ((U64)rand() << 30) ^ ((U64)rand() << 45) ^ ((U64)rand() << 60);
}

where U64 is either "__int64" or "long long", depending on platform and compiler. There is no standard integer type that is guaranteed to be 64-bits long in C++ (there is for C since C99, but not C++, at least the last time I checked).  I use compiler macros define typedef to figure this out - until an implemented standard comes out. 
 
- Phil


----- Original Message ----
From: Peter Drake <drake at lclark.edu>
To: computer-go <computer-go at computer-go.org>
Sent: Tuesday, August 8, 2006 3:32:13 PM
Subject: Re: [computer-go] C++ random numbers

How about this: 


(uint64_t(rand()) << 32) | rand()


Is the casting necessary?


Peter Drake
Assistant Professor of Computer Science
Lewis & Clark College
http://www.lclark.edu/~drake/








On Aug 8, 2006, at 3:23 PM, Chris Fant wrote:


Assuming you have a quality 32-bit generator, you can do something like:


uint64 r = (r32( )<<32) | r32( );






On 8/8/06, Peter Drake <drake at lclark.edu> wrote:


Okay, next question: how do I generate a random 64-bit unsigned integer?


Thanks in advance for any help,




Peter Drake
Assistant Professor of Computer Science
Lewis & Clark College
http://www.lclark.edu/~drake/










_______________________________________________
computer-go mailing list
computer-go at computer-go.org
http://www.computer-go.org/mailman/listinfo/computer-go/




_______________________________________________
computer-go mailing list
computer-go at computer-go.org
http://www.computer-go.org/mailman/listinfo/computer-go/


_______________________________________________
computer-go mailing list
computer-go at computer-go.org
http://www.computer-go.org/mailman/listinfo/computer-go/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://computer-go.org/pipermail/computer-go/attachments/20060808/472423e8/attachment.htm


More information about the computer-go mailing list