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 54bit implementation of Pierre L'Ecuyer's MRG32k3a pseudorandom number generator.^{4}
The functions described in this chapter are defined in the randomsource.ss file in the science collection and are made available using the form:
(require (planet "randomsource.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 randomsource 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:
(randominteger n) 
Contract: (> (integerin 1 +inf.0) naturalnumber?) 

Function:
(randomreal) 
Contract: (> (realin 0.0 1.0)) 

Variable:
defaultrandomsource 

Function:
(makerandomsource) 
Contract: (> randomsource?) 

Function:
(randomsource? x) 
Contract: (> any? boolean?) 

randomsourcestateref randomsourcestateset!
Function:
(randomsourcestateref s) 
Contract: (> randomsource? any) 
Function: (randomsourcestateset! s state) 
Contract: (> randomsource? any? any) 

Function:
(randomsourcerandomize! s) 
Contract: (> randomsource? any) 

randomsourcepseudorandomize!
Function:
(randomsourcepseudorandomize! s i j) 
Contract: (> randomsource? naturalnumber? naturalnumber> any) 

Function:
(randomsourcemakeintegers s) 
Contract: (> randomsource? procedure?) 

Function:
(randomsourcemakereals s unit) 
Function: (randomsourcemakereals s) 
Contract: (case> (> randomsource? real? procedure? (> randomsource? procedure?)) 

6.2 Additional Random Number Functionality
The science collection provides additional functionality to that provided by SRFI 27.
6.2.1 The currentrandomsource Parameter
The main additional functionality is to define a parameter^{5}, currentrandomsource, which provides a separate random source reference for each thread. The default value for this random source reference is defaultrandomstream.
The use of the currentrandomsource parameter overcomes the difficulty with assignment to defaultrandomsource. However, the routines randominteger and randomreal use the defaultrandomsource variable and are unaware of the current randomsource parameter.
Function:
(currentrandomsource s) 
Function: (currentrandomsource) 
Contract: (case> (> randomsource? any) (> randomsource?)) 

Macro:
(withrandomsource s body ...) 

Macro:
(withnewrandomsource s body ...) 

6.2.2 Uniform Random Numbers
The science collection provides alternatives to the randominteger and randomreal functions that are aware of the currentrandomsource parameter. They also provide a more convenient interface than randomsourcemakeintegers and randomsource makereals.
Function:
(randomuniformint s n) 
Function: (randomuniformint n) 
Contract: (case> (> randomsource? (integerin 1 +inf.0) naturalnumber?) (> (integerin 1 +inf.0) naturalnumber?)) 

Function:
(randomuniform s) 
Function: (randomuniform) 
Contract: (case> randomsource? (realin 0.0 1.0)) (realin 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:
(randomsourcestate s) 
Contract: (> randomsource? any) 

Function:
(setrandomsourcestate! s state) 
Contract: (> randomsource? any? any) 

6.2.4 Random Source Vectors
These function provide a convenient method for generating a vector of repeatable random sources.
Function:
(makerandomsourcevector n i) 
Function: (makerandomsourcevector n) 
Contract: (case> (> naturalnumber/c naturalnumber/c (vectorof randomsource?)) (> naturalnumber/c (vectorof randomsource?))) 

(randomsourcepseudorandomize! i j)
. If i is not provided, the i^{th} random dource is initialized using (randomsourcepseudorandomize! 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 "randomsource.ss" ("williams" "science.plt"))) (require (planet "histogramwithgraphics.ss" ("williams" "science.plt"))) (let ((h (makehistogramwithrangesuniform 40 0 1)) (s (makerandomsource))) (randomsourcerandomize! s) (withrandomsource s (do ((i 0 (+ i 1))) ((= i 10000) (void)) (histogramincrement! h (randomuniform)))) (histogramplot h "Histogram of Uniform Random Numbers"))
Figure 18 shows an example of the resulting histogram.

Example: Histogram of uniform random integers.
(require (planet "randomsource.ss" ("williams" "science.plt"))) (require (planet "histogramwithgraphics.ss" ("williams" "science.plt"))) (let ((h (makediscretehistogram)) (s (makerandomsource))) (randomsourcerandomize! s) (withrandomsource s (do ((i 0 (+ i 1))) ((= i 10000) (void)) (discretehistogramincrement! h (randomuniformint 10)))) (discretehistogramplot h "Histogram of Uniform Random Integers"))
Figure 19 shows an example of the resulting histogram.
