This SRFI defines a set of SRFI 121 generators and generator makers that yield random data of specific type and distribution. It is intended to be implemented on top of SRFI 27.
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.
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-boundCreate 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) |
random-u8-generator
random-s8-generator
random-u16-generator
random-s16-generator
random-u32-generator
random-s32-generator
random-u64-generator
random-s64-generator
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) |
random-boolean-generator
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-real-generator
lower-bound upper-boundReturns 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))) |
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
meanCreates a generator that yields real numbers from an exponential distribution with the specified mean.
make-geometric-generator
pCreates 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
LCreates a generator that yields integers from the Poisson distribution with mean L, variance L.
gsamples
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.
gweighted-samples
(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.