The Rand48 structure

The Rand48 structure implements the C Standard Library rand48.

As described in the Open Group Specification, these routines work by generating a sequence of 48-bit integer values, \(X_i\), according to the linear congruential formula:

\[ X_{i+1} = (a \cdot X_{i} + c) \mod m \qquad\text{for $i \geq 0$}\]

where

  • \(m = 2^{48}\)

  • \(a = 25214903917\)

  • \(c = 11\)

Synopsis

signature RAND48
structure Rand48 : RAND48

Interface

val seed : Word64.word -> Word64.word

val srand : word -> unit

val drand : unit -> real

val lrand : unit -> word

val mrand : unit -> int

Description

val seed : Word64.word -> Word64.word

seed s initializes the internal buffer with the 48 low-order bits from s. It returns the previous contents of the buffer.

val srand : word -> unit

srand w sets the internal buffer to (w << 16) | 0x330E.

val drand : unit -> real

drand() returns a random real number in the range \([0..1)\).

val lrand : unit -> word

lrand() returns a unsigned 31-bit random number (i.e., in the range \([0 .. 2^{31}-1\)]).

val mrand : unit -> int

mrand() returns a signed 31-bit random number (i.e., in the range \([-2^{31} .. 2^{31}-1\)]).