# SML/NJ Library Manual

## The `Random` structure

#### Synopsis

> `signature `RANDOM`
> structure `Random` :> RANDOM
> `

The Random structure implements a random number generator using a subtract-with-borrow (SWB) generator as described in Marsaglia and Zaman, "A New Class of Random Number Generators," Ann. Applied Prob. 1(3), 1991, pp. 462-480.

The SWB generator is a 31-bit generator with lags 48 and 8. It has period (2(1487) - 2(247))/105 or about 10(445). Although generally excellent, they act locally like a lagged Fibonacci generator and thus have troubles with the birthday test. To repair this, the implementation provided here incorporates a suggestion of Marsaglia and Zaman, and combines the SWB generator with a linear congruential generator, specifically (48271*a)mod(2(31)-1).

#### Interface

> `type rand
> val rand : (int * int) -> rand
> val toString : rand -> string
> val fromString : string -> rand
> val randInt : rand -> int
> val randNat : rand -> int
> val randReal : rand -> real
> val randRange : (int * int) -> rand -> int
> `

#### Description

`type rand`
  encapsulates state for the random number generator.
`rand (i, j)`
  creates the initial seed for the random number generator based on the integers i and j.
`val toString`
`val fromString`
  These functions convert between generator state and a string representation of that state. The string representation is not necessarily printable. `fromString` is only guaranteed to work on the output of `toString`, in which case `fromString o toString` is the identity function. A string in an incorrect format will cause `fromString` to raise the exception.

  These functions allow the state of a generator to be saved, in order that the generator can be replicated at a later time.

`randInt s`
  generates random integers uniformly in the range [`Int.minInt`,`Int.maxInt`] based on the information in s. As a side-effect, the function updates the internal state of s.
`randNat s`
  generates random integers uniformly in the range [`0`,`Int.maxInt`] based on the information in s. As a side-effect, the function updates the internal state of s.
`randReal s`
  generates random integers uniformly in the range (0.0,1.0) based on the information in s. As a side-effect, the function updates the internal state of s.
`randRange (i, j) s`
  generates random integers uniformly in the range [i, j] based on the information in s. As a side-effect, the function updates the internal state of s. This raises the exception Fail if j < i.

#### See Also

> Rand

[ Top | Parent | Contents | Index | Root ]

Last Modified May 6, 1998
Comments to John Reppy
Copyright © 1998 Bell Labs, Lucent Technologies