Random Number Generation
The PLT Scheme Science Collection provides additional functionality to the PLT Scheme implementation of SRFI 27 by Sabastian Egner, which, in turn, is a 54-bit implementation of Pierre L'Ecuyer's MRG32k3a pseudo-random number generator.4
The functions described in this chapter are defined in the random-source.ss file in the science collection and are made available using the form:
(require (planet "random-source.ss" ("williams" "science.plt")))
6.1 The SRFI 27 Specification
The following functions are defined in the SRFI specification and are, in turn, provided by the random-source module. The contract shows here are for documentation purposes only - the PLT Scheme implementation of SRFI 27 does not define contracts for its functions.
Function:
(random-integer n) |
Contract: (-> (integer-in 1 +inf.0) natural-number?) |
|
Function:
(random-real) |
Contract: (-> (real-in 0.0 1.0)) |
|
Variable:
default-random-source |
|
Function:
(make-random-source) |
Contract: (-> random-source?) |
|
Function:
(random-source? x) |
Contract: (-> any? boolean?) |
|
random-source-state-ref random-source-state-set!
Function:
(random-source-state-ref s) |
Contract: (-> random-source? any) |
Function: (random-source-state-set! s state) |
Contract: (-> random-source? any? any) |
|
Function:
(random-source-randomize! s) |
Contract: (-> random-source? any) |
|
random-source-pseudo-randomize!
Function:
(random-source-pseudo-randomize! s i j) |
Contract: (-> random-source? natural-number? natural-number> any) |
|
Function:
(random-source-make-integers s) |
Contract: (-> random-source? procedure?) |
|
Function:
(random-source-make-reals s unit) |
Function: (random-source-make-reals s) |
Contract: (case-> (-> random-source? real? procedure? (-> random-source? procedure?)) |
|
6.2 Additional Random Number Functionality
The science collection provides additional functionality to that provided by SRFI 27.
6.2.1 The current-random-source Parameter
The main additional functionality is to define a parameter5, current-random-source, which provides a separate random source reference for each thread. The default value for this random source reference is default-random-stream.
The use of the current-random-source parameter overcomes the difficulty with assignment to default-random-source. However, the routines random-integer and random-real use the default-random-source variable and are unaware of the current- random-source parameter.
Function:
(current-random-source s) |
Function: (current-random-source) |
Contract: (case-> (-> random-source? any) (-> random-source?)) |
|
Macro:
(with-random-source s body ...) |
|
Macro:
(with-new-random-source s body ...) |
|
6.2.2 Uniform Random Numbers
The science collection provides alternatives to the random-integer and random-real functions that are aware of the current-random-source parameter. They also provide a more convenient interface than random-source-make-integers and random-source- make-reals.
Function:
(random-uniform-int s n) |
Function: (random-uniform-int n) |
Contract: (case-> (-> random-source? (integer-in 1 +inf.0) natural-number?) (-> (integer-in 1 +inf.0) natural-number?)) |
|
Function:
(random-uniform s) |
Function: (random-uniform) |
Contract: (case-> random-source? (real-in 0.0 1.0)) (real-in 0.0 1.0))) |
|
6.2.3 Miscellaneous Functions
These functions provide an alternative set of functions to get or set the state of a random state. These functions match the conventions for structures in PLT Scheme.
Function:
(random-source-state s) |
Contract: (-> random-source? any) |
|
Function:
(set-random-source-state! s state) |
Contract: (-> random-source? any? any) |
|
6.2.4 Random Source Vectors
These function provide a convenient method for generating a vector of repeatable random sources.
Function:
(make-random-source-vector n i) |
Function: (make-random-source-vector n) |
Contract: (case-> (-> natural-number/c natural-number/c (vectorof random-source?)) (-> natural-number/c (vectorof random-source?))) |
|
(random-source-pseudo-randomize! i j)
. If i is not provided, the ith random dource is initialized using (random-source-pseudo-randomize! i 0)
. Note that this is not the same as having i default to 0.
6.3 Examples
Example: Histogram of uniform random numbers.
(require (planet "random-source.ss" ("williams" "science.plt"))) (require (planet "histogram-with-graphics.ss" ("williams" "science.plt"))) (let ((h (make-histogram-with-ranges-uniform 40 0 1)) (s (make-random-source))) (random-source-randomize! s) (with-random-source s (do ((i 0 (+ i 1))) ((= i 10000) (void)) (histogram-increment! h (random-uniform)))) (histogram-plot h "Histogram of Uniform Random Numbers"))
Figure 18 shows an example of the resulting histogram.
|
Example: Histogram of uniform random integers.
(require (planet "random-source.ss" ("williams" "science.plt"))) (require (planet "histogram-with-graphics.ss" ("williams" "science.plt"))) (let ((h (make-discrete-histogram)) (s (make-random-source))) (random-source-randomize! s) (with-random-source s (do ((i 0 (+ i 1))) ((= i 10000) (void)) (discrete-histogram-increment! h (random-uniform-int 10)))) (discrete-histogram-plot h "Histogram of Uniform Random Integers"))
Figure 19 shows an example of the resulting histogram.
|