Previous: Fixnum and Flonum Operations, Up: Numbers

MIT/GNU Scheme provides a facility for generating pseudo-random numbers. The current implementation is a “subtract-with-carry” random-number generator, based on the algorithm from A New Class of Random Number Generators, George Marsaglia and Arif Zaman, The Annals of Applied Probability, Vol. 1, No. 3, 1991. At the time it was implemented, this was a good algorithm for general purposes, but the state of the art in random-number generation is constantly changing. If necessary, the implementation will be updated to use a new algorithm while retaining the same interface.

The interface described here is very similar to that of Common Lisp.

— procedure: **random**` modulus `[`state`]

Modulusmust be a positive real number.`random`

returns a pseudo-random number between zero (inclusive) andmodulus(exclusive). The exactness of the returned number is the same as the exactness ofmodulus. Additionally, ifmodulusis an exact integer, the returned number will be also. Usually,modulusis either an exact integer or an inexact real; the current implementation has been tuned to make these two cases fast.If

stateis given and not`#f`

, it must be a random-state object; otherwise, it defaults to the value of the variable`*random-state*`

. This object is used to maintain the state of the pseudo-random-number generator and is altered as a side effect of the`random`

procedure.(random 1.0) => .32744744667719056 (random 1.0) => .01668326768172354 (random 10) => 3 (random 10) => 8 (random 100) => 38 (random 100) => 63 (random 100/3) => 130501475769920525/6755399441055744 (random 100/3) => 170571694016427575/13510798882111488

— procedure: **flo:random-unit**` state`

Statemust be a random-state object.`flo:random-unit`

returns a pseudo-random number between zero inclusive and one exclusive; the returned number is always a flonum and therefore an inexact real number.`flo:random-unit`

is equivalent to`random`

with amodulusof`1.0`

, except that it is faster.

The next three definitions concern random-state objects. In addition to
these definitions, it is important to know that random-state objects are
specifically designed so that they can be saved to disk using the
`fasdump`

procedure, and later restored using the `fasload`

procedure. This allows a particular random-state object to be saved in
order to replay a particular pseudo-random sequence.

— variable: ***random-state***

This variable holds a data structure, a random-state object, that encodes the internal state of the random-number generator that

`random`

uses by default. A call to`random`

will perform a side effect on this data structure. This variable may be changed, using`set!`

or`fluid-let`

, to hold a new random-state object.

— procedure: **make-random-state** [`state`]

This procedure returns a new random-state object, suitable for use as the value of the variable

`*random-state*`

, or as thestateargument to`random`

. Ifstateis not given or`#f`

,`make-random-state`

returns acopyof the current random-number state object (the value of the variable`*random-state*`

). Ifstateis a random-state object, a copy of that object is returned. Ifstateis`#t`

, then a new random-state object is returned that has been “randomly” initialized by some means (such as by a time-of-day clock).