Previous: , Up: Numbers   [Contents][Index]

4.8 Random 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]

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

If state is given and not #f, it must be a random-state object; otherwise, it defaults to the default-random-source. 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

State must 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 a modulus of 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 is deprecated; pass an explicit state instead.

procedure: make-random-state [state]

This procedure returns a new random-state object, suitable for use as as the state argument to random. If state is not given or #f, make-random-state returns a copy of default-random-source. If state is a random-state object, a copy of that object is returned. If state is #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).

procedure: random-state? object

Returns #t if object is a random-state object, otherwise returns #f.

Previous: , Up: Numbers   [Contents][Index]