The Random structure implements a random number generator
using the
Mersenne Twister algorithm. The implementation is specialized
to the native word size.
Note that prior to 2023.1 (and 110.99.4), this structure was implemented using a subtract-with-borrow algorithm.
Synopsis
structure Random :> RANDOM
Interface
type rand
val rand : (int * int) -> rand
val fromList : NativeWord.word list -> rand
val toBytes : rand -> Word8Vector.vector
val fromBytes : Word8Vector.vector -> rand
val toString : rand -> string
val fromString : string -> rand
val randNativeInt : rand -> NativeInt.int
val randNativeWord : rand -> NativeWord.word
val randInt : rand -> int
val randWord : rand -> int
val randNat : rand -> int
val randReal : rand -> real
val randRange : (int * int) -> rand -> int
Description
type rand-
Represents the internal state of a random number generator.
val rand : (int * int) -> rand-
rand (n1, n2)creates a random number generator from the initial seed specified by the pair(n1, n2). This function is kept for backward compatibility with the old implementation, but it is recommended that one use thefromListfunction in new code. val fromList : NativeWord.word list -> rand-
creates a random number generator from the given list of initial seeds.
val toBytes : rand -> Word8Vector.vector-
toBytes randreturns a byte vector representing the current state of the generator. val fromBytes : Word8Vector.vector -> rand-
fromBytes bvcreates a generator with the initial state that was encoded in the byte vectorbv. This expression will raiseFailexception if the byte vector is invalid. val toString : rand -> string-
toString randreturns a string representing the random-number-generator staterand. This string is aBase64encoding of the result oftoBytes rand. val fromString : string -> rand-
fromString sreturns the random-number-generator encoded as the strings(presumably generated bytoString). This expression will raiseFailexception if the stringsdoes not have the proper form. val randNativeWord : rand -> NativeWord.word-
randNativeWord randgenerates a uniform random number in the range \([0 .. 2^k-1\)], where \(k\) is the host platform’s native word size (e.g., 32 or 64). val randNativeInt : rand -> NativeInt.int-
randNativeInt randgenerates a random word a uniform distribution in the range \([0 .. 2^{k-1}-1\)], where \(k\) is the host platform’s native word size (e.g., 32 or 64). val randInt : rand -> int-
randInt randgenerates a random integer with a uniform distribution in the range \([-2^{k-1} .. 2^{k-1}-1\)], where \(k\) is the host platform’s default word size (e.g., 31 or 63). val randWord : rand -> word-
randWord randgenerates a random word with a uniform distribution in the range \([0 .. 2^k-1\)], where \(k\) is the host platform’s default word size (e.g., 31 or 63). val randNat : rand -> int-
randNat randgenerates a random integer with a uniform distribution in the range \([0 .. 2^{k-1}-1\)], where \(k\) is the host platform’s default word size (e.g., 31 or 63). val randReal : rand -> real-
randReal randgenerates a random real number in the range[0..1). val randRange : (int * int) -> rand -> int-
randRange (lo, hi) randgenerates a random number in the[lo..hi]. This function will raise theFailexception ifhi < lo.