Bill Allombert on Wed, 7 Jun 2000 23:12:57 +0200 (MET DST)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: random() weirdness


>>>>>>> Igor wrote:
>? ?random
>random({N=2^31}): random integer between 0 and N-1.

>? setrand(1);random
>1000288896
>? setrand(1);random(2^31)
>1559883374

>How can that be?  Is help lying?

Not truly. In fact the function return a random integer uniformly
distributed between 0 and N-1. which requires extra work. If N is not
given, it just return a value from the internal 31 bits RNG , which is
faster.

? #
   timer = 1 (on)
? for(i=1,100000,random)
time = 690 ms.
? for(i=1,100000,random(2^31))
time = 1,630 ms.        

you can simulate random(2^31) with

gpr()=bitor(bitand(random>>12,(1<<16)-1)<<16,bitand(random>>12,(1<<16)-1))>>1

? setrand(1);gpr()
%67 = 1559883374
? setrand(1);random(2^31)
%68 = 1559883374



Bill.