Random numbers

Started by Cyril, November 11, 2007, 11:30:40 PM

Previous topic - Next topic

Cyril

It seems that rand function doesn't accept arguments greater than 32768. Or, more precisely,  it treats any larger number as this one. Very confusing. More than, (rand n) seems to be implemented as (% (rand 32768) n), which is good for big ranges, but is a very poor solution for a 15-bit range. (E.g. average of a million (rand 10000)  samples is 4650, not 5000). In fact I have a bad feeling that the random number generator used in newLISP has much more sins which made it really unusable. Now I am trying to rewrite random.py module from the standard Python library to newLISP. It has rather good reputation.



P.S. I am using Win32 port of newLISP, maybe other platforms have better random numbers.
With newLISP you can grow your lists from the right side!

Lutz

#1
QuoteIt seems that rand function doesn't accept arguments greater than 32768




This 16 bit limitation for 'rand' is only true on Win32. On most UNIX 'rand' will do 31 bits to 2,147,483,647. It all depends on the underlying C library.



Lutz

Lutz

#2
The next development 9.2.6 version will have better integer random number generation with 'rand' on Win32.



As a workaround on the current versions use 'random' instead of 'rand' and put an 'int' wrapper around it:


(map int (random 0 10 20))
=> (3 5 5 4 6 0 5 7 3 9 5 8 7 6 0 7 8 9 8 8)


Lutz