This module defines a set of generators and generator makers that yield random data of specific type and distribution.
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 dice roller (define dice (make-random-integer-generator 1 7)) ;; Roll the dice 10 times (generator->list dice 10) ⇒ (6 6 2 4 2 5 5 1 2 2) |
make-random-u8-generatormake-random-s8-generatormake-random-u16-generatormake-random-s16-generatormake-random-u32-generatormake-random-s32-generatormake-random-u64-generatormake-random-s64-generatorUniform integer generators. Generate integers in the ranges of 8, 16, 32, and 64bit signed and unsigned integers.
(generator->list int8s 10) ⇒ (20 -101 50 -99 -111 -28 -19 -61 39 110) |
make-random-boolean-generatorGenerates boolean values (#f and #t) with equal probability.
(generator->list booleans 10) ⇒ (#f #f #t #f #f #t #f #f #f #f) |
make-random-char-generator [ char-set ]Creates 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-boundCreate a generator that generates inexact real numbers uniformly with given range. 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; 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.