~ubuntu-branches/ubuntu/trusty/gsl-ref-html/trusty

« back to all changes in this revision

Viewing changes to Random-number-generator-algorithms.html

  • Committer: Bazaar Package Importer
  • Author(s): Dirk Eddelbuettel
  • Date: 2006-04-12 19:46:32 UTC
  • mfrom: (1.3.1 upstream) (3.1.1 dapper)
  • Revision ID: james.westby@ubuntu.com-20060412194632-c9lodpl075pv9si3
Tags: 1.8-1
* New upstream release 1.8
* As with previous releases, the sources were obtained from the FSF web 
  pages by means of a wget call (c.f. the debian/rules target 'upstream')

* debian/control: Standards-Version increased to 3.6.2
* debian/copyright: Updated FSF address
* debian/rules: Set DH_COMPAT=4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<html lang="en">
 
2
<head>
 
3
<title>Random number generator algorithms - GNU Scientific Library -- Reference Manual</title>
 
4
<meta http-equiv="Content-Type" content="text/html">
 
5
<meta name="description" content="GNU Scientific Library -- Reference Manual">
 
6
<meta name="generator" content="makeinfo 4.8">
 
7
<link title="Top" rel="start" href="index.html#Top">
 
8
<link rel="up" href="Random-Number-Generation.html#Random-Number-Generation" title="Random Number Generation">
 
9
<link rel="prev" href="Reading-and-writing-random-number-generator-state.html#Reading-and-writing-random-number-generator-state" title="Reading and writing random number generator state">
 
10
<link rel="next" href="Unix-random-number-generators.html#Unix-random-number-generators" title="Unix random number generators">
 
11
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
 
12
<!--
 
13
Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 The GSL Team.
 
14
 
 
15
Permission is granted to copy, distribute and/or modify this document
 
16
under the terms of the GNU Free Documentation License, Version 1.2 or
 
17
any later version published by the Free Software Foundation; with the
 
18
Invariant Sections being ``GNU General Public License'' and ``Free Software
 
19
Needs Free Documentation'', the Front-Cover text being ``A GNU Manual'',
 
20
and with the Back-Cover Text being (a) (see below).  A copy of the
 
21
license is included in the section entitled ``GNU Free Documentation
 
22
License''.
 
23
 
 
24
(a) The Back-Cover Text is: ``You have freedom to copy and modify this
 
25
GNU Manual, like GNU software.''-->
 
26
<meta http-equiv="Content-Style-Type" content="text/css">
 
27
<style type="text/css"><!--
 
28
  pre.display { font-family:inherit }
 
29
  pre.format  { font-family:inherit }
 
30
  pre.smalldisplay { font-family:inherit; font-size:smaller }
 
31
  pre.smallformat  { font-family:inherit; font-size:smaller }
 
32
  pre.smallexample { font-size:smaller }
 
33
  pre.smalllisp    { font-size:smaller }
 
34
  span.sc    { font-variant:small-caps }
 
35
  span.roman { font-family:serif; font-weight:normal; } 
 
36
  span.sansserif { font-family:sans-serif; font-weight:normal; } 
 
37
--></style>
 
38
</head>
 
39
<body>
 
40
<div class="node">
 
41
<p>
 
42
<a name="Random-number-generator-algorithms"></a>
 
43
Next:&nbsp;<a rel="next" accesskey="n" href="Unix-random-number-generators.html#Unix-random-number-generators">Unix random number generators</a>,
 
44
Previous:&nbsp;<a rel="previous" accesskey="p" href="Reading-and-writing-random-number-generator-state.html#Reading-and-writing-random-number-generator-state">Reading and writing random number generator state</a>,
 
45
Up:&nbsp;<a rel="up" accesskey="u" href="Random-Number-Generation.html#Random-Number-Generation">Random Number Generation</a>
 
46
<hr>
 
47
</div>
 
48
 
 
49
<h3 class="section">17.9 Random number generator algorithms</h3>
 
50
 
 
51
<p>The functions described above make no reference to the actual algorithm
 
52
used.  This is deliberate so that you can switch algorithms without
 
53
having to change any of your application source code.  The library
 
54
provides a large number of generators of different types, including
 
55
simulation quality generators, generators provided for compatibility
 
56
with other libraries and historical generators from the past.
 
57
 
 
58
   <p>The following generators are recommended for use in simulation.  They
 
59
have extremely long periods, low correlation and pass most statistical
 
60
tests.
 
61
 
 
62
<div class="defun">
 
63
&mdash; Generator: <b>gsl_rng_mt19937</b><var><a name="index-gsl_005frng_005fmt19937-1391"></a></var><br>
 
64
<blockquote><p><a name="index-MT19937-random-number-generator-1392"></a>The MT19937 generator of Makoto Matsumoto and Takuji Nishimura is a
 
65
variant of the twisted generalized feedback shift-register algorithm,
 
66
and is known as the &ldquo;Mersenne Twister&rdquo; generator.  It has a Mersenne
 
67
prime period of
 
68
<!-- {$2^{19937} - 1$} -->
 
69
2^19937 - 1 (about
 
70
<!-- {$10^{6000}$} -->
 
71
10^6000) and is
 
72
equi-distributed in 623 dimensions.  It has passed the <span class="sc">diehard</span>
 
73
statistical tests.  It uses 624 words of state per generator and is
 
74
comparable in speed to the other generators.  The original generator used
 
75
a default seed of 4357 and choosing <var>s</var> equal to zero in
 
76
<code>gsl_rng_set</code> reproduces this.
 
77
 
 
78
        <p>For more information see,
 
79
          <ul>
 
80
<li>Makoto Matsumoto and Takuji Nishimura, &ldquo;Mersenne Twister: A
 
81
623-dimensionally equidistributed uniform pseudorandom number
 
82
generator&rdquo;. <cite>ACM Transactions on Modeling and Computer
 
83
Simulation</cite>, Vol. 8, No. 1 (Jan. 1998), Pages 3&ndash;30
 
84
</ul>
 
85
 
 
86
     <p class="noindent">The generator <code>gsl_rng_mt19937</code> uses the second revision of the
 
87
seeding procedure published by the two authors above in 2002.  The
 
88
original seeding procedures could cause spurious artifacts for some seed
 
89
values. They are still available through the alternative generators
 
90
<code>gsl_rng_mt19937_1999</code> and <code>gsl_rng_mt19937_1998</code>. 
 
91
</p></blockquote></div>
 
92
 
 
93
<div class="defun">
 
94
&mdash; Generator: <b>gsl_rng_ranlxs0</b><var><a name="index-gsl_005frng_005franlxs0-1393"></a></var><br>
 
95
&mdash; Generator: <b>gsl_rng_ranlxs1</b><var><a name="index-gsl_005frng_005franlxs1-1394"></a></var><br>
 
96
&mdash; Generator: <b>gsl_rng_ranlxs2</b><var><a name="index-gsl_005frng_005franlxs2-1395"></a></var><br>
 
97
<blockquote><p><a name="index-RANLXS-random-number-generator-1396"></a>
 
98
The generator <code>ranlxs0</code> is a second-generation version of the
 
99
<span class="sc">ranlux</span> algorithm of L&uuml;scher, which produces &ldquo;luxury random
 
100
numbers&rdquo;.  This generator provides single precision output (24 bits) at
 
101
three luxury levels <code>ranlxs0</code>, <code>ranlxs1</code> and <code>ranlxs2</code>. 
 
102
It uses double-precision floating point arithmetic internally and can be
 
103
significantly faster than the integer version of <code>ranlux</code>,
 
104
particularly on 64-bit architectures.  The period of the generator is
 
105
about <!-- {$10^{171}$} -->
 
106
10^171.  The algorithm has mathematically proven properties and
 
107
can provide truly decorrelated numbers at a known level of randomness. 
 
108
The higher luxury levels provide increased decorrelation between samples
 
109
as an additional safety margin. 
 
110
</p></blockquote></div>
 
111
 
 
112
<div class="defun">
 
113
&mdash; Generator: <b>gsl_rng_ranlxd1</b><var><a name="index-gsl_005frng_005franlxd1-1397"></a></var><br>
 
114
&mdash; Generator: <b>gsl_rng_ranlxd2</b><var><a name="index-gsl_005frng_005franlxd2-1398"></a></var><br>
 
115
<blockquote><p><a name="index-RANLXD-random-number-generator-1399"></a>
 
116
These generators produce double precision output (48 bits) from the
 
117
<span class="sc">ranlxs</span> generator.  The library provides two luxury levels
 
118
<code>ranlxd1</code> and <code>ranlxd2</code>. 
 
119
</p></blockquote></div>
 
120
 
 
121
<div class="defun">
 
122
&mdash; Generator: <b>gsl_rng_ranlux</b><var><a name="index-gsl_005frng_005franlux-1400"></a></var><br>
 
123
&mdash; Generator: <b>gsl_rng_ranlux389</b><var><a name="index-gsl_005frng_005franlux389-1401"></a></var><br>
 
124
<blockquote>
 
125
<p><a name="index-RANLUX-random-number-generator-1402"></a>The <code>ranlux</code> generator is an implementation of the original
 
126
algorithm developed by L&uuml;scher.  It uses a
 
127
lagged-fibonacci-with-skipping algorithm to produce &ldquo;luxury random
 
128
numbers&rdquo;.  It is a 24-bit generator, originally designed for
 
129
single-precision IEEE floating point numbers.  This implementation is
 
130
based on integer arithmetic, while the second-generation versions
 
131
<span class="sc">ranlxs</span> and <span class="sc">ranlxd</span> described above provide floating-point
 
132
implementations which will be faster on many platforms. 
 
133
The period of the generator is about <!-- {$10^{171}$} -->
 
134
10^171.  The algorithm has mathematically proven properties and
 
135
it can provide truly decorrelated numbers at a known level of
 
136
randomness.  The default level of decorrelation recommended by L&uuml;scher
 
137
is provided by <code>gsl_rng_ranlux</code>, while <code>gsl_rng_ranlux389</code>
 
138
gives the highest level of randomness, with all 24 bits decorrelated. 
 
139
Both types of generator use 24 words of state per generator.
 
140
 
 
141
        <p>For more information see,
 
142
          <ul>
 
143
<li>M. L&uuml;scher, &ldquo;A portable high-quality random number generator for
 
144
lattice field theory calculations&rdquo;, <cite>Computer Physics
 
145
Communications</cite>, 79 (1994) 100&ndash;110. 
 
146
<li>F. James, &ldquo;RANLUX: A Fortran implementation of the high-quality
 
147
pseudo-random number generator of L&uuml;scher&rdquo;, <cite>Computer Physics
 
148
Communications</cite>, 79 (1994) 111&ndash;114
 
149
</ul>
 
150
        </p></blockquote></div>
 
151
 
 
152
<div class="defun">
 
153
&mdash; Generator: <b>gsl_rng_cmrg</b><var><a name="index-gsl_005frng_005fcmrg-1403"></a></var><br>
 
154
<blockquote><p><a name="index-CMRG_002c-combined-multiple-recursive-random-number-generator-1404"></a>This is a combined multiple recursive generator by L'Ecuyer. 
 
155
Its sequence is,
 
156
where the two underlying generators x_n and y_n are,
 
157
with coefficients
 
158
a_1 = 0,
 
159
a_2 = 63308,
 
160
a_3 = -183326,
 
161
b_1 = 86098,
 
162
b_2 = 0,
 
163
b_3 = -539608,
 
164
and moduli
 
165
<!-- {$m_1 = 2^{31} - 1 = 2147483647$} -->
 
166
m_1 = 2^31 - 1 = 2147483647
 
167
and
 
168
<!-- {$m_2 = 2145483479$} -->
 
169
m_2 = 2145483479.
 
170
 
 
171
        <p>The period of this generator is
 
172
<!-- {$\hbox{lcm}(m_1^3-1, m_2^3-1)$} -->
 
173
lcm(m_1^3-1, m_2^3-1),
 
174
which is approximately
 
175
<!-- {$2^{185}$} -->
 
176
2^185
 
177
(about
 
178
<!-- {$10^{56}$} -->
 
179
10^56).  It uses
 
180
6 words of state per generator.  For more information see,
 
181
 
 
182
          <ul>
 
183
<li>P. L'Ecuyer, &ldquo;Combined Multiple Recursive Random Number
 
184
Generators&rdquo;, <cite>Operations Research</cite>, 44, 5 (1996), 816&ndash;822. 
 
185
</ul>
 
186
        </p></blockquote></div>
 
187
 
 
188
<div class="defun">
 
189
&mdash; Generator: <b>gsl_rng_mrg</b><var><a name="index-gsl_005frng_005fmrg-1405"></a></var><br>
 
190
<blockquote><p><a name="index-MRG_002c-multiple-recursive-random-number-generator-1406"></a>This is a fifth-order multiple recursive generator by L'Ecuyer, Blouin
 
191
and Coutre.  Its sequence is,
 
192
with
 
193
a_1 = 107374182,
 
194
a_2 = a_3 = a_4 = 0,
 
195
a_5 = 104480
 
196
and
 
197
<!-- {$m = 2^{31}-1$} -->
 
198
m = 2^31 - 1.
 
199
 
 
200
        <p>The period of this generator is about
 
201
<!-- {$10^{46}$} -->
 
202
10^46.  It uses 5 words
 
203
of state per generator.  More information can be found in the following
 
204
paper,
 
205
          <ul>
 
206
<li>P. L'Ecuyer, F. Blouin, and R. Coutre, &ldquo;A search for good multiple
 
207
recursive random number generators&rdquo;, <cite>ACM Transactions on Modeling and
 
208
Computer Simulation</cite> 3, 87&ndash;98 (1993). 
 
209
</ul>
 
210
        </p></blockquote></div>
 
211
 
 
212
<div class="defun">
 
213
&mdash; Generator: <b>gsl_rng_taus</b><var><a name="index-gsl_005frng_005ftaus-1407"></a></var><br>
 
214
&mdash; Generator: <b>gsl_rng_taus2</b><var><a name="index-gsl_005frng_005ftaus2-1408"></a></var><br>
 
215
<blockquote><p><a name="index-Tausworthe-random-number-generator-1409"></a>This is a maximally equidistributed combined Tausworthe generator by
 
216
L'Ecuyer.  The sequence is,
 
217
where,
 
218
computed modulo
 
219
<!-- {$2^{32}$} -->
 
220
2^32.  In the formulas above
 
221
<!-- {$\oplus$} -->
 
222
^^
 
223
denotes &ldquo;exclusive-or&rdquo;.  Note that the algorithm relies on the properties
 
224
of 32-bit unsigned integers and has been implemented using a bitmask
 
225
of <code>0xFFFFFFFF</code> to make it work on 64 bit machines.
 
226
 
 
227
        <p>The period of this generator is <!-- {$2^{88}$} -->
 
228
2^88 (about
 
229
<!-- {$10^{26}$} -->
 
230
10^26).  It uses 3 words of state per generator.  For more
 
231
information see,
 
232
 
 
233
          <ul>
 
234
<li>P. L'Ecuyer, &ldquo;Maximally Equidistributed Combined Tausworthe
 
235
Generators&rdquo;, <cite>Mathematics of Computation</cite>, 65, 213 (1996), 203&ndash;213. 
 
236
</ul>
 
237
 
 
238
     <p class="noindent">The generator <code>gsl_rng_taus2</code> uses the same algorithm as
 
239
<code>gsl_rng_taus</code> but with an improved seeding procedure described in
 
240
the paper,
 
241
 
 
242
          <ul>
 
243
<li>P. L'Ecuyer, &ldquo;Tables of Maximally Equidistributed Combined LFSR
 
244
Generators&rdquo;, <cite>Mathematics of Computation</cite>, 68, 225 (1999), 261&ndash;269
 
245
</ul>
 
246
 
 
247
     <p class="noindent">The generator <code>gsl_rng_taus2</code> should now be used in preference to
 
248
<code>gsl_rng_taus</code>. 
 
249
</p></blockquote></div>
 
250
 
 
251
<div class="defun">
 
252
&mdash; Generator: <b>gsl_rng_gfsr4</b><var><a name="index-gsl_005frng_005fgfsr4-1410"></a></var><br>
 
253
<blockquote><p><a name="index-Four_002dtap-Generalized-Feedback-Shift-Register-1411"></a>The <code>gfsr4</code> generator is like a lagged-fibonacci generator, and
 
254
produces each number as an <code>xor</code>'d sum of four previous values.
 
255
 
 
256
        <p>Ziff (ref below) notes that &ldquo;it is now widely known&rdquo; that two-tap
 
257
registers (such as R250, which is described below)
 
258
have serious flaws, the most obvious one being the three-point
 
259
correlation that comes from the definition of the generator.  Nice
 
260
mathematical properties can be derived for GFSR's, and numerics bears
 
261
out the claim that 4-tap GFSR's with appropriately chosen offsets are as
 
262
random as can be measured, using the author's test.
 
263
 
 
264
        <p>This implementation uses the values suggested the example on p392 of
 
265
Ziff's article: A=471, B=1586, C=6988, D=9689.
 
266
 
 
267
        <p>If the offsets are appropriately chosen (such as the one ones in this
 
268
implementation), then the sequence is said to be maximal; that means
 
269
that the period is 2^D - 1, where D is the longest lag. 
 
270
(It is one less than 2^D because it is not permitted to have all
 
271
zeros in the <code>ra[]</code> array.)  For this implementation with
 
272
D=9689 that works out to about <!-- {$10^{2917}$} -->
 
273
10^2917.
 
274
 
 
275
        <p>Note that the implementation of this generator using a 32-bit
 
276
integer amounts to 32 parallel implementations of one-bit
 
277
generators.  One consequence of this is that the period of this
 
278
32-bit generator is the same as for the one-bit generator. 
 
279
Moreover, this independence means that all 32-bit patterns are
 
280
equally likely, and in particular that 0 is an allowed random
 
281
value.  (We are grateful to Heiko Bauke for clarifying for us these
 
282
properties of GFSR random number generators.)
 
283
 
 
284
        <p>For more information see,
 
285
          <ul>
 
286
<li>Robert M. Ziff, &ldquo;Four-tap shift-register-sequence random-number
 
287
generators&rdquo;, <cite>Computers in Physics</cite>, 12(4), Jul/Aug
 
288
1998, pp 385&ndash;392. 
 
289
</ul>
 
290
        </p></blockquote></div>
 
291
 
 
292
   </body></html>
 
293