Professional

Home
Learn Shen
Videos
Community Wiki
Community
OS Kernel
OS Library
Shen Professional


More Help

Concurrency
FTP
Graphics
Shilbert
Standard Extension
Web
 

 

The Random Number Library

(v.1.1 25-06-16)
W O Riha

1. Random Number Generators

Designing a ‘good and efficient’ random number generator (RNG) is not an easy task! A lot has been written in the literature (click here), and a large number of techniques have been developed (see, for example, List of Random Number Generators).

We provide two simple methods.

Method 1: (see http://en.wikipedia.org/wiki/Combined_Linear_Congruential_Generator)
Method 2: This method is due to D. Knuth (see
http://en.wikipedia.org/wiki/Linear_congruential_generator)

It is simpler and computationally cheaper, but perhaps less ‘random’. The function names all have the suffix ‘Knuth’ – no examples are quoted in the following description.

Note: If only a few random numbers are required, either method will be satisfactory. Where an application depends on hundreds of thousands random numbers, it might pay to make use of the LISP random number generator which is considerably faster.

1. Random Number Generators

2. Seeding the Random Number Generators

3. Random Number Functions

2. Seeding the Random Number Generators

There are three seeds for seeding the random number generators:

random.*seed1*
random.*seed2*

These seed the random function. The Knuth versions are seeded by random.*seed*. We suggest (set <seed> (get-time unix)) for seeding these globals.

3. Random Number Functions

random01 : --> number
random01-Knuth : --> number

Input: none
Output: a random number in the interval [0 1)

(random01)
0.42611619002179996 : number

(random01)
0.24454672298695493 : number

(random01)
0.7647986388801989 : number

rand : number --> number --> number
rand-Knuth : number --> number --> number

Input: two numbers A and B (A < B)
Output: a random number in the interval [A B)

(rand -1 1)
-0.5656633158593354 : number

(rand -1 1)
0.5141242093874876 : number

(rand 0 1000)
893.1472259189534 : number

random-int : number --> number --> number
random-int-Knuth : number --> number --> number

Input: two numbers A and B (A < B)
Output: a random integer in the interval [A B)

(random-int 1 11) \\ between 1 and 10 (inclusive)
4 : number

(random-int 1 11)
7 : number

random : number --> number --> number
random-Knuth : number --> number --> number

Input: two numbers A and B (A < B)
Output: if both A and B are integers, a random integer in the interval [A B) otherwise a random number in [A B).
Note: this function is ‘more expensive’!

(random -1 12)
9 : number

(random -1 12.001)
3.287118902233013 : number