Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: boundschecking

Author: Dieter Buerssner

Date: 12:58:56 11/04/02

Go up one level in this thread


On November 04, 2002 at 15:18:04, Severi Salminen wrote:

>>>>__int64 rand64()
>>>>{
>>>>	int a,b,c,d,e;
>>>>	a=rand();
>>>>	b=rand()<<15;
>>>>	c=rand()<<30;
>>>>	d=rand()<<45;
>>>>	e=rand()<<60;
>>>>
>>>>	return a^((__int64)b)^((__int64)c)^
>>>>		((__int64)d)^((__int64)e);
>>>>}
>>>
>>>Under most systems, d and e will be zero.
>
>A few points. As Dieter said, the above is wrong, because of too late type
>casts. Secondly why do you use XOR? Why not OR, the result is about the same? OR
>has a definite sequence points and can be used like in your original rand64():

No, binary or does also not guarantee the order in which the different parts are
called. You are thinking of logical or.

>unsigned __int64 rand64()
>{
>    return (ui) rand()|(ui)rand()<<15|(ui)rand()<<30 //and so on...
>}


It is even worse. There is no guarantee, that rand() returns exactly 15 bits
(there is a guarantee, that it returns at least 15 bits). So, if an
implementation will return (say) 31 bits, you will have in average much more set
bits in the 64 bit number, than 0 bits. Not very random ...
When using xor, it at least gives something rather random looking back.

OTOH for any slightly serious simulation, one would of course never use a system
rand().

I also agree with all the other posters, who suggest to use your own PRNG or use
a program generated table.

Regards,
Dieter




This page took 0.01 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.