Random numbers

For the Compleat Fan
Locked
Cyril
Posts: 183
Joined: Tue Oct 30, 2007 6:27 pm
Location: Moscow, Russia
Contact:

Random numbers

Post by 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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

It 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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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:

Code: Select all

(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

Locked