This site is a static rendering of the Trac instance that was used by R7RS-WG1 for its work on R7RS-small (PDF), which was ratified in 2013. For more information, see Home. For a version of this page that may be more recent, see AdvancedRandomGauche in WG2's repo for R7RS-large.


2017-06-16 01:33:28

This SRFI defines a set of SRFI 121 generators and generator makers that yield random data of specific types and distributions. It is intended to be implemented on top of SRFI 27.

Random source parameter

random-source-parameter [Variable]

A SRFI 39 or R7RS parameter whose value, when invoked as a thunk, is the random source used by the procedures in this SRFI. By using parameterize, it is possible to substitute another random source during the dynamic extent of the body.

Generators of primitive data types

Those generators generate uniformly distributed data.

In the following examples, we use the generator->list procedure to show some concrete data from the generators.

make-random-integer-generator lower-bound upper-bound

Create exact integer generators. Creates a generator that generates integers between lower-bound (inclusive) and upper-bound (exclusive) uniformly.

;; A die roller
(define die (make-random-integer-generator 1 7))

;; Roll the die 10 times
(generator->list die 10)
 ⇒ (6 6 2 4 2 5 5 1 2 2)

Uniform integer generators that generate integers in the ranges of 8, 16, 32, and 64-bit signed and unsigned integers.

(generator->list random-s8-generator 10)
 ⇒ (20 -101 50 -99 -111 -28 -19 -61 39 110)

Generates boolean values (#f and #t) with equal probability.

(generator->list random-boolean-generator 10)
 ⇒ (#f #f #t #f #f #t #f #f #f #f)
make-random-char-generator [ char-set ]

Returns a generator that generates characters in the SRFI 14 char-set uniformly. The default char-set is [A-Za-z0-9].

(define alphanumeric-chars (make-random-char-generator))

(generator->list alphanumeric-chars 10)
 ⇒ (#\f #\m #\3 #\S #\z #\m #\x #\S #\l #\y)
make-random-string-generator [ k char-set ]

Returns a generator that generates random strings whose characters are in the SRFI 14 char-set. The default char-set is [A-Za-z0-9]. The strings are all less than k characters in length.

make-random-real-generator lower-bound upper-bound

Returns a generator that generates inexact real numbers uniformly. The procedure returns reals between lower-bound and upper-bound, both exclusive.

(define uniform-100 (make-random-real-generator 0 100))

(generator->list uniform-100 10)
 ⇒ (81.67965004942268 81.84927577572596 53.02443813660833)

Note that a generator from make-random-real-generator can generate the upper-bound, as opposed to make-random-integer-generator. If you need to exclude the bound value, just discard it; SRFI 121 gfilter may come handy.

(define generate-from-0-below-1
  (gfilter (lambda (r) (not (= r 1.0))) (make-random-real-generator 0.0 1.0)))

Nonuniform distributions

make-normal-generator [ mean [ deviation ] ]

Creates a generator that yields real numbers from a normal distribution with the specified mean and deviation. The default value of mean is 0.0 and deviation is 1.0.

make-exponential-generator mean

Creates a generator that yields real numbers from an exponential distribution with the specified mean.

make-geometric-generator p

Creates a generator that yields integers from the geometric distribution with success probability p (0 <= p <= 1). The mean is 1/p and variance is (1-p)/p^2.

make-poisson-generator L

Creates a generator that yields integers from the Poisson distribution with mean L, variance L.

Generator operations

make-sampling-generator generator ...

Takes the generators and returns a new generator. Every time the resulting generator is called, it picks one of the input generators with equal probability, then calls it to get a value.

make-weighted-sampling-generator (weight generator) ...

The arguments alternate between nonnegative real numbers and generators. The real number determines the weight, or the relative probability that the generator which follows it is chosen. The sum of the weights doesn’t need to be 1.0.