The Rand
structure provides a simple random number generator as described
in Larry Paulson in ML for the Working Programmer (pp. 170-171).
The original algorithm was recommended by Park and Miller in
Random number generators: good ones
are hard to find, CACM 1988 (pp 1192-1201) with modifications described
in CACM 1993 (pp. 105-110).
Note: it is recommended that one use the Random
structure when the quality of the generated numbers is at all important.
The main advantages of this implementation is that it is functional
(the generators provided by the Random
structure are imperative) and
it is fast.
Synopsis
structure Rand
Interface
type rand = Word.word
val randMin : rand
val randMax : rand
val random : rand -> rand
val mkRandom : rand -> unit -> rand
val norm : rand -> real
val range : (int * int) -> rand -> int
Description
type rand = Word.word
-
The "state" of the generator, which is just a single word.
val randMin : rand
-
The minimum allowed value for the state.
val randMax : rand
-
The maximum allowed value for the state.
val random : rand -> rand
-
random seed
returns a pseudo-random value in the range[randMin .. randMax]
. Iteratively using the value returned byrandom
as the next seed will produce a sequence of pseudo-random numbers. val mkRandom : rand -> unit -> rand
-
mkRandom seed
returns a function that generates a fresh random number in the range[randMin .. randMax]
. val norm : rand -> real
-
norm rand
maps the random number in the range[randMin .. randMax]
to the real interval(0..1)
. val range : (int * int) -> rand -> int
-
range (lo, hi) rand
maps the random number in the range[randMin .. randMax]
to the interval[lo..hi]
. This function will raise theFail
exception ifhi < lo
.
Bugs
This implementation needs to be updated for 64-bit systems.