Igor Schein on Wed, 21 Jun 2000 11:35:00 -0400


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

Re: random() weirdness


On Wed, Jun 07, 2000 at 11:12:57PM +0200, Bill Allombert wrote:
> >>>>>>> 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

First of all, how is above better than just using random%2^31?

Second, I think the following behavior should be documented, if it's
indeed intended:

? for(k=0,7,setrand(1+k*2^28);print(random(2^31)))
1559883374
1559883374
1559883374
1559883374
1559883374
1559883374
1559883374
1559883374

I ran into this after many CPU hours.  Of course I would have looked
in the code if I had a slight suspicion beforehand :)

Thanks

Igor