# SML/NJ Library Manual

## The `Rand` structure

#### Synopsis

```signature RAND structure Rand : RAND ```

The Rand structure implements a simple linear congruential random number generator with a uniform distribution. It is based on the one described in Paulson's book (pp. 170-171), which is derived from the article ``Random number generators: good ones are hard to find,'' by Stephen K. Park and Keith W. Miller, CACM 31 (1988), pp. 1192-1201. It also incorporates the changes suggested by Park and Miller in CACM 36 (1993), pp. 105-110. The generator has period 2(31)-1. The values returned by `random` and `mkRandom` are 31-bit words in the range [`randMin`,`randMax`]. In the current implementation, `randMin` is 1.0 and `randMax` is 2(31)-2.

#### Interface

```type rand = Word31.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`

`randMin`
`randMax`
specify the minimum and maximum values returned by `random` and `mkRandom`.

```random r ```
returns a random number in the range [`randMin`,`randMax`] using the seed r. Iteratively using the value returned by `random` as the next seed to `random` will produce a sequence of pseudo-random numbers.

```mkRandom r ```
returns a stateful function generating a sequence of random numbers in the range [`randMin`,`randMax`], starting from the seed r.

`val norm`
Uniformly maps the range [`randMin`,`randMax`] to the range (0.0,1.0). This can be used in combination with `random` or `mkRandom` to generate normalized real-valued random numbers.

```range (i, j) ```
returns a function mapping the range [`randMin`,`randMax`] uniformly to the range [i,j]. This can be used in combination with `random` or `mkRandom` to generate random numbers in a given integer range. This raises the exception Fail if j < i.