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
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.
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
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 seedreturns a pseudo-random value in the range
[randMin .. randMax]. Iteratively using the value returned by
randomas the next seed will produce a sequence of pseudo-random numbers.
val mkRandom : rand -> unit -> rand
something (* Given seed, return function generating a sequence of
random numbers randMin ⇐ v ⇐ randMax *)
val norm : rand -> real
norm randmaps the random number in the range
[randMin .. randMax]to the real interval
val range : (int * int) -> rand -> int
range (lo, hi) randmaps the random number in the range
[randMin .. randMax]to the interval
[lo..hi]. This function will raise the
hi < lo.
This implementation needs to be updated for 64-bit systems.