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.
Modulus must be a positive real number.
randomreturns 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 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
randomprocedure.(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
State must be a random-state object.
flo:random-unitreturns 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-unitis equivalent to
randomwith 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
procedure. This allows a particular random-state object to be saved in
order to replay a particular pseudo-random sequence.
This variable holds a data structure, a random-state object, that encodes the internal state of the random-number generator that
randomuses by default. A call to
randomwill perform a side effect on this data structure. This variable may be changed, using
fluid-let, to hold a new random-state object.
This procedure returns a new random-state object, suitable for use as the value of the variable
*random-state*, or as the state argument to
random. If state is not given or
make-random-statereturns a copy of the current random-number state object (the value of the variable
*random-state*). 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).