📄 rng.texi
字号:
@math{2^19937 - 1} (about
@c{$10^{6000}$}
@math{10^6000}) and is
equi-distributed in 623 dimensions. It has passed the @sc{diehard}
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} equal to zero in
@code{gsl_rng_set} reproduces this.
For more information see,
@itemize @asis
@item
Makoto Matsumoto and Takuji Nishimura, "Mersenne Twister: A
623-dimensionally equidistributed uniform pseudorandom number
generator". @cite{ACM Transactions on Modeling and Computer
Simulation}, Vol. 8, No. 1 (Jan. 1998), Pages 3-30
@end itemize
@noindent
The generator @code{gsl_rng_19937} 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 alternate generators
@code{gsl_rng_mt19937_1999} and @code{gsl_rng_mt19937_1998}.
@end deffn
@deffn {Generator} gsl_rng_ranlxs0
@deffnx {Generator} gsl_rng_ranlxs1
@deffnx {Generator} gsl_rng_ranlxs2
@cindex RANLXS random number generator
The generator @code{ranlxs0} is a second-generation version of the
@sc{ranlux} algorithm of L@"uscher, which produces "luxury random
numbers". This generator provides single precision output (24 bits) at
three luxury levels @code{ranlxs0}, @code{ranlxs1} and @code{ranlxs2}.
It uses double-precision floating point arithmetic internally and can be
significantly faster than the integer version of @code{ranlux},
particularly on 64-bit architectures. The period of the generator is
about @c{$10^{171}$}
@math{10^171}. The algorithm has mathematically proven properties and
can provide truly decorrelated numbers at a known level of randomness.
The higher luxury levels provide additional decorrelation between samples
as an additional safety margin.
@end deffn
@deffn {Generator} gsl_rng_ranlxd1
@deffnx {Generator} gsl_rng_ranlxd2
@cindex RANLXD random number generator
These generators produce double precision output (48 bits) from the
@sc{ranlxs} generator. The library provides two luxury levels
@code{ranlxd1} and @code{ranlxd2}.
@end deffn
@deffn {Generator} gsl_rng_ranlux
@deffnx {Generator} gsl_rng_ranlux389
@cindex RANLUX random number generator
The @code{ranlux} generator is an implementation of the original
algorithm developed by L@"uscher. It uses a
lagged-fibonacci-with-skipping algorithm to produce "luxury random
numbers". 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
@sc{ranlxs} and @sc{ranlxd} described above provide floating-point
implementations which will be faster on many platforms.
The period of the generator is about @c{$10^{171}$}
@math{10^171}. 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@"uscher
is provided by @code{gsl_rng_ranlux}, while @code{gsl_rng_ranlux389}
gives the highest level of randomness, with all 24 bits decorrelated.
Both types of generator use 24 words of state per generator.
For more information see,
@itemize @asis
@item
M. L@"uscher, "A portable high-quality random number generator for
lattice field theory calculations", @cite{Computer Physics
Communications}, 79 (1994) 100-110.
@item
F. James, "RANLUX: A Fortran implementation of the high-quality
pseudo-random number generator of L@"uscher", @cite{Computer Physics
Communications}, 79 (1994) 111-114
@end itemize
@end deffn
@deffn {Generator} gsl_rng_cmrg
@cindex CMRG, combined multiple recursive random number generator
This is a combined multiple recursive generator by L'Ecuyer.
Its sequence is,
@tex
\beforedisplay
$$
z_n = (x_n - y_n) \,\hbox{mod}\, m_1
$$
\afterdisplay
@end tex
@ifinfo
@example
z_n = (x_n - y_n) mod m_1
@end example
@end ifinfo
@noindent
where the two underlying generators @math{x_n} and @math{y_n} are,
@tex
\beforedisplay
$$
\eqalign{
x_n & = (a_1 x_{n-1} + a_2 x_{n-2} + a_3 x_{n-3}) \,\hbox{mod}\, m_1 \cr
y_n & = (b_1 y_{n-1} + b_2 y_{n-2} + b_3 y_{n-3}) \,\hbox{mod}\, m_2
}
$$
\afterdisplay
@end tex
@ifinfo
@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
@end example
@end ifinfo
@noindent
with coefficients
@math{a_1 = 0},
@math{a_2 = 63308},
@math{a_3 = -183326},
@math{b_1 = 86098},
@math{b_2 = 0},
@math{b_3 = -539608},
and moduli
@c{$m_1 = 2^{31} - 1 = 2147483647$}
@math{m_1 = 2^31 - 1 = 2147483647}
and
@c{$m_2 = 2145483479$}
@math{m_2 = 2145483479}.
The period of this generator is
@c{$2^{205}$}
@math{2^205}
(about
@c{$10^{61}$}
@math{10^61}). It uses
6 words of state per generator. For more information see,
@itemize @asis
@item
P. L'Ecuyer, "Combined Multiple Recursive Random Number
Generators," @cite{Operations Research}, 44, 5 (1996), 816--822.
@end itemize
@end deffn
@deffn {Generator} gsl_rng_mrg
@cindex MRG, multiple recursive random number generator
This is a fifth-order multiple recursive generator by L'Ecuyer, Blouin
and Coutre. Its sequence is,
@tex
\beforedisplay
$$
x_n = (a_1 x_{n-1} + a_5 x_{n-5}) \,\hbox{mod}\, m
$$
\afterdisplay
@end tex
@ifinfo
@example
x_n = (a_1 x_@{n-1@} + a_5 x_@{n-5@}) mod m
@end example
@end ifinfo
@noindent
with
@math{a_1 = 107374182},
@math{a_2 = a_3 = a_4 = 0},
@math{a_5 = 104480}
and
@c{$m = 2^{31}-1$}
@math{m = 2^31 - 1}.
The period of this generator is about
@c{$10^{46}$}
@math{10^46}. It uses 5 words
of state per generator. More information can be found in the following
paper,
@itemize @asis
@item
P. L'Ecuyer, F. Blouin, and R. Coutre, "A search for good multiple
recursive random number generators", @cite{ACM Transactions on Modeling and
Computer Simulation} 3, 87-98 (1993).
@end itemize
@end deffn
@deffn {Generator} gsl_rng_taus
@deffnx {Generator} gsl_rng_taus2
@cindex Tausworthe random number generator
This is a maximally equidistributed combined Tausworthe generator by
L'Ecuyer. The sequence is,
@tex
\beforedisplay
$$
x_n = (s^1_n \oplus s^2_n \oplus s^3_n)
$$
\afterdisplay
@end tex
@ifinfo
@example
x_n = (s1_n ^^ s2_n ^^ s3_n)
@end example
@end ifinfo
@noindent
where,
@tex
\beforedisplay
$$
\eqalign{
s^1_{n+1} &= (((s^1_n \& 4294967294)\ll 12) \oplus (((s^1_n\ll 13) \oplus s^1_n)\gg 19)) \cr
s^2_{n+1} &= (((s^2_n \& 4294967288)\ll 4) \oplus (((s^2_n\ll 2) \oplus s^2_n)\gg 25)) \cr
s^3_{n+1} &= (((s^3_n \& 4294967280)\ll 17) \oplus (((s^3_n\ll 3) \oplus s^3_n)\gg 11))
}
$$
\afterdisplay
@end tex
@ifinfo
@example
s1_@{n+1@} = (((s1_n&4294967294)<<12)^^(((s1_n<<13)^^s1_n)>>19))
s2_@{n+1@} = (((s2_n&4294967288)<< 4)^^(((s2_n<< 2)^^s2_n)>>25))
s3_@{n+1@} = (((s3_n&4294967280)<<17)^^(((s3_n<< 3)^^s3_n)>>11))
@end example
@end ifinfo
@noindent
computed modulo
@c{$2^{32}$}
@math{2^32}. In the formulas above
@c{$\oplus$}
@math{^^}
denotes ``exclusive-or''. Note that the algorithm relies on the properties
of 32-bit unsigned integers and has been implemented using a bitmask
of @code{0xFFFFFFFF} to make it work on 64 bit machines.
The period of this generator is @c{$2^{88}$}
@math{2^88} (about
@c{$10^{26}$}
@math{10^26}). It uses 3 words of state per generator. For more
information see,
@itemize @asis
@item
P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
Generators", @cite{Mathematics of Computation}, 65, 213 (1996), 203--213.
@end itemize
@noindent
The generator @code{gsl_rng_taus2} uses the same algorithm as
@code{gsl_rng_taus} but with an improved seeding procedure described in
the paper,
@itemize @asis
@item
P. L'Ecuyer, "Tables of Maximally Equidistributed Combined LFSR
Generators", @cite{Mathematics of Computation}, 68, 225 (1999), 261--269
@end itemize
@noindent
The generator @code{gsl_rng_taus2} should now be used in preference to
@code{gsl_rng_taus}.
@end deffn
@deffn {Generator} gsl_rng_gfsr4
@cindex Four-tap Generalized Feedback Shift Register
The @code{gfsr4} generator is like a lagged-fibonacci generator, and
produces each number as an @code{xor}'d sum of four previous values.
@tex
\beforedisplay
$$
r_n = r_{n-A} \oplus r_{n-B} \oplus r_{n-C} \oplus r_{n-D}
$$
\afterdisplay
@end tex
@ifinfo
@example
r_n = r_@{n-A@} ^^ r_@{n-B@} ^^ r_@{n-C@} ^^ r_@{n-D@}
@end example
@end ifinfo
Ziff (ref below) notes that "it is now widely known" 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's, and numerics bears
out the claim that 4-tap GFSR's with appropriately chosen offsets are as
random as can be measured, using the author's test.
This implementation uses the values suggested the example on p392 of
Ziff's article: @math{A=471}, @math{B=1586}, @math{C=6988}, @math{D=9689}.
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 @math{2^D - 1}, where @math{D} is the longest lag.
(It is one less than @math{2^D} because it is not permitted to have all
zeros in the @code{ra[]} array.) For this implementation with
@math{D=9689} that works out to about @c{$10^{2917}$}
@math{10^2917}.
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 inedpendence 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.)
For more information see,
@itemize @asis
@item
Robert M. Ziff, "Four-tap shift-register-sequence random-number
generators", @cite{Computers in Physics}, 12(4), Jul/Aug
1998, pp 385-392.
@end itemize
@end deffn
@node Unix random number generators
@section Unix random number generators
The standard Unix random number generators @code{rand}, @code{random}
and @code{rand48} are provided as part of GSL. Although these
generators are widely available individually often they aren't all
available on the same platform. This makes it difficult to write
portable code using them and so we have included the complete set of
Unix generators in GSL for convenience. Note that these generators
don't produce high-quality randomness and aren't suitable for work
requiring accurate statistics. However, if you won't be measuring
statistical quantities and just want to introduce some variation into
your program then these generators are quite acceptable.
@cindex BSD random number generator, rand
@cindex Unix random number generators, rand
@cindex Unix random number generators, rand48
@deffn {Generator} gsl_rng_rand
@cindex BSD random number generator
This is the BSD @code{rand()} generator. Its sequence is
@tex
\beforedisplay
$$
x_{n+1} = (a x_n + c) \,\hbox{mod}\, m
$$
\afterdisplay
@end tex
@ifinfo
@example
x_@{n+1@} = (a x_n + c) mod m
@end example
@end ifinfo
@noindent
with
@math{a = 1103515245},
@math{c = 12345} and
@c{$m = 2^{31}$}
@math{m = 2^31}.
The seed specifies the initial value,
@math{x_1}. The period of this
generator is
@c{$2^{31}$}
@math{2^31}, and it uses 1 word of storage per
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -