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.

Advanced­Random­Gauche

cowan
2014-11-23 10:40:42
1history
source

This module defines a set of generators and generator makers that yield random data of specific type and distribution.

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 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-generator
make-random-s8-generator
make-random-u16-generator
make-random-s16-generator
make-random-u32-generator
make-random-s32-generator
make-random-u64-generator
make-random-s64-generator

Uniform 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-generator

Generates 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-bound

Create 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)))

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.

Aggregate data generator operations

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.

gpermutations vector

Returns a generator that yields random permutations of vector.

 
(generator->list (permutations-of '(1 2 3)) 3)
 ⇒ ((1 2 3) (2 3 1) (3 2 1))

(generator->list (gpermutations "abc") 3)
 ⇒ ("cba" "cba" "cab")
gcombinations size vector

Returns a generator that yields a vector of size elements randomly picked from vector.

 
(generator->list (gcombinations 2 #(a b c)) 5)
 ⇒ (#(a c) #(a b) #(a c) #(b a) #(a c))

(generator->list (combinations-of 2 '#(a b c)) 5)
 ⇒ (#(a c) #(b c) #(c b) #(b a) #(b c))