File: Random-number-generator-algorithms.html

package info (click to toggle)
gsl-ref-html 2.3-1
  • links: PTS
  • area: non-free
  • in suites: bullseye, buster, sid
  • size: 6,876 kB
  • ctags: 4,574
  • sloc: makefile: 35
file content (357 lines) | stat: -rw-r--r-- 16,938 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 The GSL Team.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "GNU General Public License" and "Free Software
Needs Free Documentation", the Front-Cover text being "A GNU Manual",
and with the Back-Cover Text being (a) (see below). A copy of the
license is included in the section entitled "GNU Free Documentation
License".

(a) The Back-Cover Text is: "You have the freedom to copy and modify this
GNU Manual." -->
<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Scientific Library &ndash; Reference Manual: Random number generator algorithms</title>

<meta name="description" content="GNU Scientific Library &ndash; Reference Manual: Random number generator algorithms">
<meta name="keywords" content="GNU Scientific Library &ndash; Reference Manual: Random number generator algorithms">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Function-Index.html#Function-Index" rel="index" title="Function Index">
<link href="Random-Number-Generation.html#Random-Number-Generation" rel="up" title="Random Number Generation">
<link href="Unix-random-number-generators.html#Unix-random-number-generators" rel="next" title="Unix random number generators">
<link href="Reading-and-writing-random-number-generator-state.html#Reading-and-writing-random-number-generator-state" rel="previous" title="Reading and writing random number generator state">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>


</head>

<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Random-number-generator-algorithms"></a>
<div class="header">
<p>
Next: <a href="Unix-random-number-generators.html#Unix-random-number-generators" accesskey="n" rel="next">Unix random number generators</a>, Previous: <a href="Reading-and-writing-random-number-generator-state.html#Reading-and-writing-random-number-generator-state" accesskey="p" rel="previous">Reading and writing random number generator state</a>, Up: <a href="Random-Number-Generation.html#Random-Number-Generation" accesskey="u" rel="up">Random Number Generation</a> &nbsp; [<a href="Function-Index.html#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Random-number-generator-algorithms-1"></a>
<h3 class="section">18.9 Random number generator algorithms</h3>

<p>The functions described above make no reference to the actual algorithm
used.  This is deliberate so that you can switch algorithms without
having to change any of your application source code.  The library
provides a large number of generators of different types, including
simulation quality generators, generators provided for compatibility
with other libraries and historical generators from the past.
</p>
<p>The following generators are recommended for use in simulation.  They
have extremely long periods, low correlation and pass most statistical
tests.  For the most reliable source of uncorrelated numbers, the
second-generation <small>RANLUX</small> generators have the strongest proof of
randomness.
</p>
<dl>
<dt><a name="index-gsl_005frng_005fmt19937"></a>Generator: <strong>gsl_rng_mt19937</strong></dt>
<dd><a name="index-MT19937-random-number-generator"></a>
<p>The MT19937 generator of Makoto Matsumoto and Takuji Nishimura is a
variant of the twisted generalized feedback shift-register algorithm,
and is known as the &ldquo;Mersenne Twister&rdquo; generator.  It has a Mersenne
prime period of 
<em>2^19937 - 1</em> (about 
<em>10^6000</em>) and is
equi-distributed in 623 dimensions.  It has passed the <small>DIEHARD</small>
statistical tests.  It uses 624 words of state per generator and is
comparable in speed to the other generators.  The original generator used
a default seed of 4357 and choosing <var>s</var> equal to zero in
<code>gsl_rng_set</code> reproduces this.  Later versions switched to 5489
as the default seed, you can choose this explicitly via <code>gsl_rng_set</code> 
instead if you require it.
</p>
<p>For more information see,
</p><ul class="no-bullet">
<li><!-- /@w --> Makoto Matsumoto and Takuji Nishimura, &ldquo;Mersenne Twister: A
623-dimensionally equidistributed uniform pseudorandom number
generator&rdquo;. <cite>ACM Transactions on Modeling and Computer
Simulation</cite>, Vol. 8, No. 1 (Jan. 1998), Pages 3&ndash;30
</li></ul>

<p>The generator <code>gsl_rng_mt19937</code> uses the second revision of the
seeding procedure published by the two authors above in 2002.  The
original seeding procedures could cause spurious artifacts for some seed
values. They are still available through the alternative generators
<code>gsl_rng_mt19937_1999</code> and <code>gsl_rng_mt19937_1998</code>.
</p></dd></dl>

<dl>
<dt><a name="index-gsl_005frng_005franlxs0"></a>Generator: <strong>gsl_rng_ranlxs0</strong></dt>
<dt><a name="index-gsl_005frng_005franlxs1"></a>Generator: <strong>gsl_rng_ranlxs1</strong></dt>
<dt><a name="index-gsl_005frng_005franlxs2"></a>Generator: <strong>gsl_rng_ranlxs2</strong></dt>
<dd><a name="index-RANLXS-random-number-generator"></a>

<p>The generator <code>ranlxs0</code> is a second-generation version of the
<small>RANLUX</small> algorithm of L&uuml;scher, which produces &ldquo;luxury random
numbers&rdquo;.  This generator provides single precision output (24 bits) at
three luxury levels <code>ranlxs0</code>, <code>ranlxs1</code> and <code>ranlxs2</code>, 
in increasing order of strength.  
It uses double-precision floating point arithmetic internally and can be
significantly faster than the integer version of <code>ranlux</code>,
particularly on 64-bit architectures.  The period of the generator is
about <em>10^171</em>.  The algorithm has mathematically proven properties and
can provide truly decorrelated numbers at a known level of randomness.
The higher luxury levels provide increased decorrelation between samples
as an additional safety margin.
</p>
<p>Note that the range of allowed seeds for this generator is <em>[0,2^31-1]</em>. Higher seed values are wrapped modulo <em>2^31</em>.
</p></dd></dl>

<dl>
<dt><a name="index-gsl_005frng_005franlxd1"></a>Generator: <strong>gsl_rng_ranlxd1</strong></dt>
<dt><a name="index-gsl_005frng_005franlxd2"></a>Generator: <strong>gsl_rng_ranlxd2</strong></dt>
<dd><a name="index-RANLXD-random-number-generator"></a>

<p>These generators produce double precision output (48 bits) from the
<small>RANLXS</small> generator.  The library provides two luxury levels
<code>ranlxd1</code> and <code>ranlxd2</code>, in increasing order of strength.
</p></dd></dl>


<dl>
<dt><a name="index-gsl_005frng_005franlux"></a>Generator: <strong>gsl_rng_ranlux</strong></dt>
<dt><a name="index-gsl_005frng_005franlux389"></a>Generator: <strong>gsl_rng_ranlux389</strong></dt>
<dd>
<a name="index-RANLUX-random-number-generator"></a>
<p>The <code>ranlux</code> generator is an implementation of the original
algorithm developed by L&uuml;scher.  It uses a
lagged-fibonacci-with-skipping algorithm to produce &ldquo;luxury random
numbers&rdquo;.  It is a 24-bit generator, originally designed for
single-precision IEEE floating point numbers.  This implementation is
based on integer arithmetic, while the second-generation versions
<small>RANLXS</small> and <small>RANLXD</small> described above provide floating-point
implementations which will be faster on many platforms.
The period of the generator is about <em>10^171</em>.  The algorithm has mathematically proven properties and
it can provide truly decorrelated numbers at a known level of
randomness.  The default level of decorrelation recommended by L&uuml;scher
is provided by <code>gsl_rng_ranlux</code>, while <code>gsl_rng_ranlux389</code>
gives the highest level of randomness, with all 24 bits decorrelated.
Both types of generator use 24 words of state per generator.
</p>
<p>For more information see,
</p><ul class="no-bullet">
<li><!-- /@w --> M. L&uuml;scher, &ldquo;A portable high-quality random number generator for
lattice field theory calculations&rdquo;, <cite>Computer Physics
Communications</cite>, 79 (1994) 100&ndash;110.
</li><li><!-- /@w --> F. James, &ldquo;RANLUX: A Fortran implementation of the high-quality
pseudo-random number generator of L&uuml;scher&rdquo;, <cite>Computer Physics
Communications</cite>, 79 (1994) 111&ndash;114
</li></ul>
</dd></dl>


<dl>
<dt><a name="index-gsl_005frng_005fcmrg"></a>Generator: <strong>gsl_rng_cmrg</strong></dt>
<dd><a name="index-CMRG_002c-combined-multiple-recursive-random-number-generator"></a>
<p>This is a combined multiple recursive generator by L&rsquo;Ecuyer. 
Its sequence is,
</p>
<div class="example">
<pre class="example">z_n = (x_n - y_n) mod m_1
</pre></div>

<p>where the two underlying generators <em>x_n</em> and <em>y_n</em> are,
</p>
<div class="example">
<pre class="example">x_n = (a_1 x_{n-1} + a_2 x_{n-2} + a_3 x_{n-3}) mod m_1
y_n = (b_1 y_{n-1} + b_2 y_{n-2} + b_3 y_{n-3}) mod m_2
</pre></div>

<p>with coefficients 
<em>a_1 = 0</em>, 
<em>a_2 = 63308</em>, 
<em>a_3 = -183326</em>,
<em>b_1 = 86098</em>, 
<em>b_2 = 0</em>,
<em>b_3 = -539608</em>,
and moduli 
<em>m_1 = 2^31 - 1 = 2147483647</em>
and 
<em>m_2 = 2145483479</em>.
</p>
<p>The period of this generator is  
<em>lcm(m_1^3-1, m_2^3-1)</em>,
which is approximately
<em>2^185</em> 
(about 
<em>10^56</em>).  It uses
6 words of state per generator.  For more information see,
</p>
<ul class="no-bullet">
<li><!-- /@w --> P. L&rsquo;Ecuyer, &ldquo;Combined Multiple Recursive Random Number
Generators&rdquo;, <cite>Operations Research</cite>, 44, 5 (1996), 816&ndash;822.
</li></ul>
</dd></dl>

<dl>
<dt><a name="index-gsl_005frng_005fmrg"></a>Generator: <strong>gsl_rng_mrg</strong></dt>
<dd><a name="index-MRG_002c-multiple-recursive-random-number-generator"></a>
<p>This is a fifth-order multiple recursive generator by L&rsquo;Ecuyer, Blouin
and Coutre.  Its sequence is,
</p>
<div class="example">
<pre class="example">x_n = (a_1 x_{n-1} + a_5 x_{n-5}) mod m
</pre></div>

<p>with 
<em>a_1 = 107374182</em>, 
<em>a_2 = a_3 = a_4 = 0</em>, 
<em>a_5 = 104480</em>
and 
<em>m = 2^31 - 1</em>.
</p>
<p>The period of this generator is about 
<em>10^46</em>.  It uses 5 words
of state per generator.  More information can be found in the following
paper,
</p><ul class="no-bullet">
<li><!-- /@w --> P. L&rsquo;Ecuyer, F. Blouin, and R. Coutre, &ldquo;A search for good multiple
recursive random number generators&rdquo;, <cite>ACM Transactions on Modeling and
Computer Simulation</cite> 3, 87&ndash;98 (1993).
</li></ul>
</dd></dl>

<dl>
<dt><a name="index-gsl_005frng_005ftaus"></a>Generator: <strong>gsl_rng_taus</strong></dt>
<dt><a name="index-gsl_005frng_005ftaus2"></a>Generator: <strong>gsl_rng_taus2</strong></dt>
<dd><a name="index-Tausworthe-random-number-generator"></a>
<p>This is a maximally equidistributed combined Tausworthe generator by
L&rsquo;Ecuyer.  The sequence is,
</p>
<div class="example">
<pre class="example">x_n = (s1_n ^^ s2_n ^^ s3_n) 
</pre></div>

<p>where,
</p>
<div class="example">
<pre class="example">s1_{n+1} = (((s1_n&amp;4294967294)&lt;&lt;12)^^(((s1_n&lt;&lt;13)^^s1_n)&gt;&gt;19))
s2_{n+1} = (((s2_n&amp;4294967288)&lt;&lt; 4)^^(((s2_n&lt;&lt; 2)^^s2_n)&gt;&gt;25))
s3_{n+1} = (((s3_n&amp;4294967280)&lt;&lt;17)^^(((s3_n&lt;&lt; 3)^^s3_n)&gt;&gt;11))
</pre></div>

<p>computed modulo 
<em>2^32</em>.  In the formulas above 
<em>^^</em>
denotes &ldquo;exclusive-or&rdquo;.  Note that the algorithm relies on the properties
of 32-bit unsigned integers and has been implemented using a bitmask
of <code>0xFFFFFFFF</code> to make it work on 64 bit machines.
</p>
<p>The period of this generator is <em>2^88</em> (about
<em>10^26</em>).  It uses 3 words of state per generator.  For more
information see,
</p>
<ul class="no-bullet">
<li><!-- /@w --> P. L&rsquo;Ecuyer, &ldquo;Maximally Equidistributed Combined Tausworthe
Generators&rdquo;, <cite>Mathematics of Computation</cite>, 65, 213 (1996), 203&ndash;213.
</li></ul>

<p>The generator <code>gsl_rng_taus2</code> uses the same algorithm as
<code>gsl_rng_taus</code> but with an improved seeding procedure described in
the paper,
</p>
<ul class="no-bullet">
<li><!-- /@w --> P. L&rsquo;Ecuyer, &ldquo;Tables of Maximally Equidistributed Combined LFSR
Generators&rdquo;, <cite>Mathematics of Computation</cite>, 68, 225 (1999), 261&ndash;269
</li></ul>

<p>The generator <code>gsl_rng_taus2</code> should now be used in preference to
<code>gsl_rng_taus</code>.
</p></dd></dl>

<dl>
<dt><a name="index-gsl_005frng_005fgfsr4"></a>Generator: <strong>gsl_rng_gfsr4</strong></dt>
<dd><a name="index-Four_002dtap-Generalized-Feedback-Shift-Register"></a>
<p>The <code>gfsr4</code> generator is like a lagged-fibonacci generator, and 
produces each number as an <code>xor</code>&rsquo;d sum of four previous values.
</p>
<div class="example">
<pre class="example">r_n = r_{n-A} ^^ r_{n-B} ^^ r_{n-C} ^^ r_{n-D}
</pre></div>

<p>Ziff (ref below) notes that &ldquo;it is now widely known&rdquo; that two-tap
registers (such as R250, which is described below)
have serious flaws, the most obvious one being the three-point
correlation that comes from the definition of the generator.  Nice
mathematical properties can be derived for GFSR&rsquo;s, and numerics bears
out the claim that 4-tap GFSR&rsquo;s with appropriately chosen offsets are as
random as can be measured, using the author&rsquo;s test.
</p>
<p>This implementation uses the values suggested the example on p392 of
Ziff&rsquo;s article: <em>A=471</em>, <em>B=1586</em>, <em>C=6988</em>, <em>D=9689</em>.
</p>

<p>If the offsets are appropriately chosen (such as the one ones in this
implementation), then the sequence is said to be maximal; that means
that the period is <em>2^D - 1</em>, where <em>D</em> is the longest lag.
(It is one less than <em>2^D</em> because it is not permitted to have all
zeros in the <code>ra[]</code> array.)  For this implementation with
<em>D=9689</em> that works out to about <em>10^2917</em>.
</p>
<p>Note that the implementation of this generator using a 32-bit
integer amounts to 32 parallel implementations of one-bit
generators.  One consequence of this is that the period of this
32-bit generator is the same as for the one-bit generator.
Moreover, this independence means that all 32-bit patterns are
equally likely, and in particular that 0 is an allowed random
value.  (We are grateful to Heiko Bauke for clarifying for us these
properties of GFSR random number generators.)
</p>
<p>For more information see,
</p><ul class="no-bullet">
<li><!-- /@w --> Robert M. Ziff, &ldquo;Four-tap shift-register-sequence random-number 
generators&rdquo;, <cite>Computers in Physics</cite>, 12(4), Jul/Aug
1998, pp 385&ndash;392.
</li></ul>
</dd></dl>

<hr>
<div class="header">
<p>
Next: <a href="Unix-random-number-generators.html#Unix-random-number-generators" accesskey="n" rel="next">Unix random number generators</a>, Previous: <a href="Reading-and-writing-random-number-generator-state.html#Reading-and-writing-random-number-generator-state" accesskey="p" rel="previous">Reading and writing random number generator state</a>, Up: <a href="Random-Number-Generation.html#Random-Number-Generation" accesskey="u" rel="up">Random Number Generation</a> &nbsp; [<a href="Function-Index.html#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>