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.

Source for wiki AdvancedRandomGauche version 1

author

cowan

comment


    

ipnr

127.11.51.1

name

AdvancedRandomGauche

readonly

0

text

{{{
#!html
<p>This module defines a set of generators and generator makers
that yield random data of specific type and distribution.
</p></dd></dl>

<a name="Generators-of-primitive-data-types"></a>
<h3 class="subheading">Generators of primitive data types</h3>

<p>Those generators generate uniformly distributed data.
</p>
<p>In the following examples, we use the <code>generator-&gt;list</code> procedure to show
some concrete data from the generators.
</p>
<dl>
<dt><a name="index-integers_002dbetween_0024"></a><code>make-random-integer-generator</code><i> lower-bound upper-bound</i></dt>
<dd><p>Create exact integer generators.  Creates a generator that generates integers between
<var>lower-bound</var> (inclusive) and <var>upper-bound</var> (exclusive) uniformly.
</p>
<table><tr><td>&nbsp;</td><td><pre class="example">;; A dice roller
(define dice (make-random-integer-generator 1 7))

;; Roll the dice 10 times
(generator-&gt;list dice 10)
 &rArr; (6 6 2 4 2 5 5 1 2 2)
</pre></td></tr></table>
</dd></dl>

<dl>
<dt><a name="index-int8s"></a><code>make-random-u8-generator</code></dt>
<dt><a name="index-uint8s"></a><code>make-random-s8-generator</code></dt>
<dt><a name="index-int16s"></a><code>make-random-u16-generator</code></dt>
<dt><a name="index-uint16s"></a><code>make-random-s16-generator</code></dt>
<dt><a name="index-int32s"></a><code>make-random-u32-generator</code></dt>
<dt><a name="index-uint32s"></a><code>make-random-s32-generator</code></dt>
<dt><a name="index-int64s"></a><code>make-random-u64-generator</code></dt>
<dt><a name="index-uint64s"></a><code>make-random-s64-generator</code></dt>
<dd><p>Uniform integer generators.  Generate integers in the ranges of
8, 16, 32, and 64bit signed and unsigned integers.
</p>
<table><tr><td>&nbsp;</td><td><pre class="example">(generator-&gt;list int8s 10)
 &rArr; (20 -101 50 -99 -111 -28 -19 -61 39 110)
</pre></td></tr></table>
</dd></dl>

<dl>
<dt><a name="index-booleans"></a><code>make-random-boolean-generator</code></dt>
<dd><p>Generates boolean values (<code>#f</code> and <code>#t</code>) with equal probability.
</p>
<table><tr><td>&nbsp;</td><td><pre class="example">(generator-&gt;list booleans 10)
 &rArr; (#f #f #t #f #f #t #f #f #f #f)
</pre></td></tr></table>
</dd></dl>

<dl>
<dt><a name="index-chars_0024"></a><code>make-random-char-generator</code><i> [ char-set ]</i></dt>
<dd><p>Creates a generator that generates characters in the SRFI 14 <var>char-set</var> uniformly.
The default <var>char-set</var> is <code>[A-Za-z0-9]</code>.
</p>
<table><tr><td>&nbsp;</td><td><pre class="example">(define alphanumeric-chars (make-random-char-generator))

(generator-&gt;list alphanumeric-chars 10)
 &rArr; (#\f #\m #\3 #\S #\z #\m #\x #\S #\l #\y)
</pre></td></tr></table>
</dd></dl>

<dl>
<dt><a name="index-reals_002dbetween_0024"></a><code>make-random-real-generator</code><i> lower-bound upper-bound</i></dt>
<dd><p>Create a generator that generates inexact real numbers uniformly with given range.
The procedure returns reals between
lower-bound and upper-bound, both exclusive.
</p>
<table><tr><td>&nbsp;</td><td><pre class="example">(define uniform-100 (make-random-real-generator 0 100))

(generator-&gt;list uniform-100 10)
 &rArr; (81.67965004942268 81.84927577572596 53.02443813660833)
</pre></td></tr></table>

<p>Note that a generator from <code>make-random-real-generator</code> can generate the upper-bound, as opposed to <code>make-random-integer-generator</code>.  If you need to exclude
the bound value, just discard it; <code>gfilter</code> may come
handy.
</p>
<table><tr><td>&nbsp;</td><td><pre class="example">(define generate-from-0-below-1
  (gfilter (lambda (r) (not (= r 1.0))) (make-random-real-generator$ 0.0 1.0)))
</pre></td></tr></table>
</dd></dl>

<a name="Nonuniform-distributions"></a>
<h3 class="subheading">Nonuniform distributions</h3>

<dl>
<dt><a name="index-reals_002dnormal_0024"></a><code>make-normal-generator</code><i> [ mean [ deviation ] ]</i></dt>
<dd><p>Creates a generator that yields real numbers from a normal distribution
with the specified <var>mean</var> and <var>deviation</var>.  The default value of <var>mean</var> is 0.0
and <var>deviation</var> is 1.0.
</p></dd></dl>

<dl>
<dt><a name="index-reals_002dexponential_0024"></a><code>make-exponential-generator</code><i> mean</i></dt>
<dd><p>Creates a generator that yields real numbers from an exponential distribution
with the specified <var>mean</var>.
</p></dd></dl>

<dl>
<dt><a name="index-integers_002dgeometric_0024"></a><code>make-geometric-generator</code><i> p</i></dt>
<dd><p>Creates a generator that yields integers from the geometric distribution
with success probability <var>p</var> (0 &lt;= p &lt;= 1).  The mean is <code>1/p</code> and
variance is <code>(1-p)/p^2</code>.
</p></dd></dl>

<dl>
<dt><a name="index-integers_002dpoisson_0024"></a><code>make-poisson-generator</code><i> L</i></dt>
<dd><p>Creates a generator that yields integers from the Poisson distribution with
mean <var>L</var>, variance <var>L</var>.
</p></dd></dl>

<a name="Generator "></a>
<h3 class="subheading">Aggregate data generator operations</h3>

<dl>
<dt><a name="index-samples_002dfrom"></a><code>gsamples</code><i> generator</i> ...</dt>
<dd><p>Takes the <i>generators</i>
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.
</p></dd></dl>

<dl>
<dt><a name="index-weighted_002dsamples_002dfrom"></a><code>gweighted-samples</code> (<i>weight generator</i>) ...</dt>
<dd><p>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&rsquo;t need to
be 1.0.
</p></dd></dl>

<dl>
<dt><a name="index-permutations_002dof"></a><code>gpermutations</code><i> vector</i></dt>
<dd><p>Returns a generator that yields random permutations of <var>vector</var>.
</p>
<table><tr><td>&nbsp;</td><td><pre class="example">(generator-&gt;list (permutations-of '(1 2 3)) 3)
 &rArr; ((1 2 3) (2 3 1) (3 2 1))

(generator-&gt;list (gpermutations &quot;abc&quot;) 3)
 &rArr; (&quot;cba&quot; &quot;cba&quot; &quot;cab&quot;)
</pre></td></tr></table>
</dd></dl>

<dl>
<dt><a name="index-combinations_002dof"></a><code>gcombinations</code><i> size vector</i></dt>
<dd><p>Returns a generator that yields a vector of <var>size</var> elements
randomly picked from <var>vector</var>.
</p>
<table><tr><td>&nbsp;</td><td><pre class="example">(generator-&gt;list (gcombinations 2 #(a b c)) 5)
 &rArr; (#(a c) #(a b) #(a c) #(b a) #(a c))

(generator-&gt;list (combinations-of 2 '#(a b c)) 5)
 &rArr; (#(a c) #(b c) #(c b) #(b a) #(b c))
</pre></td></tr></table>
</dd></dl>
}}}

time

2014-11-23 10:40:42

version

1