rng.texi

来自「math library from gnu」· TEXI 代码 · 共 1,466 行 · 第 1/4 页

TEXI
1,466
字号
@deftypefun int gsl_rng_fread (FILE * @var{stream}, gsl_rng * @var{r})This function reads the random number state into the random numbergenerator @var{r} from the open stream @var{stream} in binary format.The random number generator @var{r} must be preinitialized with thecorrect random number generator type since type information is notsaved.  The return value is 0 for success and @code{GSL_EFAILED} ifthere was a problem reading from the file.  The data is assumed tohave been written in the native binary format on the samearchitecture.@end deftypefun@node Random number generator algorithms@section Random number generator algorithmsThe functions described above make no reference to the actual algorithmused.  This is deliberate so that you can switch algorithms withouthaving to change any of your application source code.  The libraryprovides a large number of generators of different types, includingsimulation quality generators, generators provided for compatibilitywith other libraries and historical generators from the past.The following generators are recommended for use in simulation.  Theyhave extremely long periods, low correlation and pass most statisticaltests.  For the most reliable source of uncorrelated numbers, thesecond-generation @sc{ranlux} generators have the strongest proof ofrandomness.@deffn {Generator} gsl_rng_mt19937@cindex MT19937 random number generatorThe MT19937 generator of Makoto Matsumoto and Takuji Nishimura is avariant of the twisted generalized feedback shift-register algorithm,and is known as the ``Mersenne Twister'' generator.  It has a Mersenneprime period of @comment@c{$2^{19937} - 1$} @math{2^19937 - 1} (about @c{$10^{6000}$}@math{10^6000}) and isequi-distributed in 623 dimensions.  It has passed the @sc{diehard}statistical tests.  It uses 624 words of state per generator and iscomparable in speed to the other generators.  The original generator useda default seed of 4357 and choosing @var{s} equal to zero in@code{gsl_rng_set} reproduces this.  Later versions switched to 5489as the default seed, you can choose this explicitly via @code{gsl_rng_set} instead if you require it.For more information see,@itemize @asis@itemMakoto Matsumoto and Takuji Nishimura, ``Mersenne Twister: A623-dimensionally equidistributed uniform pseudorandom numbergenerator''. @cite{ACM Transactions on Modeling and ComputerSimulation}, Vol.@: 8, No.@: 1 (Jan. 1998), Pages 3--30@end itemize@noindentThe generator @code{gsl_rng_mt19937} uses the second revision of theseeding procedure published by the two authors above in 2002.  Theoriginal seeding procedures could cause spurious artifacts for some seedvalues. They are still available through the alternative 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 generatorThe generator @code{ranlxs0} is a second-generation version of the@sc{ranlux} algorithm of L@"uscher, which produces ``luxury randomnumbers''.  This generator provides single precision output (24 bits) atthree luxury levels @code{ranlxs0}, @code{ranlxs1} and @code{ranlxs2}, in increasing order of strength.It uses double-precision floating point arithmetic internally and can besignificantly faster than the integer version of @code{ranlux},particularly on 64-bit architectures.  The period of the generator isabout @c{$10^{171}$} @math{10^171}.  The algorithm has mathematically proven properties andcan provide truly decorrelated numbers at a known level of randomness.The higher luxury levels provide increased decorrelation between samplesas an additional safety margin.@end deffn@deffn {Generator} gsl_rng_ranlxd1@deffnx {Generator} gsl_rng_ranlxd2@cindex RANLXD random number generatorThese generators produce double precision output (48 bits) from the@sc{ranlxs} generator.  The library provides two luxury levels@code{ranlxd1} and @code{ranlxd2}, in increasing order of strength.@end deffn@deffn {Generator} gsl_rng_ranlux@deffnx {Generator} gsl_rng_ranlux389@cindex RANLUX random number generatorThe @code{ranlux} generator is an implementation of the originalalgorithm developed by L@"uscher.  It uses alagged-fibonacci-with-skipping algorithm to produce ``luxury randomnumbers''.  It is a 24-bit generator, originally designed forsingle-precision IEEE floating point numbers.  This implementation isbased on integer arithmetic, while the second-generation versions@sc{ranlxs} and @sc{ranlxd} described above provide floating-pointimplementations 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 andit can provide truly decorrelated numbers at a known level ofrandomness.  The default level of decorrelation recommended by L@"uscheris 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@itemM. L@"uscher, ``A portable high-quality random number generator forlattice field theory calculations'', @cite{Computer PhysicsCommunications}, 79 (1994) 100--110.@itemF. James, ``RANLUX: A Fortran implementation of the high-qualitypseudo-random number generator of L@"uscher'', @cite{Computer PhysicsCommunications}, 79 (1994) 111--114@end itemize@end deffn@deffn {Generator} gsl_rng_cmrg@cindex CMRG, combined multiple recursive random number generatorThis 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@examplez_n = (x_n - y_n) mod m_1@end example@end ifinfo@noindentwhere 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 \cry_n & = (b_1 y_{n-1} + b_2 y_{n-2} + b_3 y_{n-3}) \,\hbox{mod}\, m_2}$$\afterdisplay@end tex@ifinfo@examplex_n = (a_1 x_@{n-1@} + a_2 x_@{n-2@} + a_3 x_@{n-3@}) mod m_1y_n = (b_1 y_@{n-1@} + b_2 y_@{n-2@} + b_3 y_@{n-3@}) mod m_2@end example@end ifinfo@noindentwith 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{$\hbox{lcm}(m_1^3-1, m_2^3-1)$}@math{lcm(m_1^3-1, m_2^3-1)},which is approximately@c{$2^{185}$}@math{2^185} (about @c{$10^{56}$}@math{10^56}).  It uses6 words of state per generator.  For more information see,@itemize @asis@itemP. L'Ecuyer, ``Combined Multiple Recursive Random NumberGenerators'', @cite{Operations Research}, 44, 5 (1996), 816--822.@end itemize@end deffn@deffn {Generator} gsl_rng_mrg@cindex MRG, multiple recursive random number generatorThis is a fifth-order multiple recursive generator by L'Ecuyer, Blouinand 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@examplex_n = (a_1 x_@{n-1@} + a_5 x_@{n-5@}) mod m@end example@end ifinfo@noindentwith @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 wordsof state per generator.  More information can be found in the followingpaper,@itemize @asis@itemP. L'Ecuyer, F. Blouin, and R. Coutre, ``A search for good multiplerecursive random number generators'', @cite{ACM Transactions on Modeling andComputer Simulation} 3, 87--98 (1993).@end itemize@end deffn@deffn {Generator} gsl_rng_taus@deffnx {Generator} gsl_rng_taus2@cindex Tausworthe random number generatorThis is a maximally equidistributed combined Tausworthe generator byL'Ecuyer.  The sequence is,@tex\beforedisplay$$x_n = (s^1_n \oplus s^2_n \oplus s^3_n) $$\afterdisplay@end tex@ifinfo@examplex_n = (s1_n ^^ s2_n ^^ s3_n) @end example@end ifinfo@noindentwhere,@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)) \crs^2_{n+1} &= (((s^2_n \& 4294967288)\ll 4) \oplus (((s^2_n\ll 2) \oplus s^2_n)\gg 25)) \crs^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@examples1_@{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@noindentcomputed modulo @c{$2^{32}$}@math{2^32}.  In the formulas above @c{$\oplus$}@math{^^}denotes ``exclusive-or''.  Note that the algorithm relies on the propertiesof 32-bit unsigned integers and has been implemented using a bitmaskof @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 moreinformation see,@itemize @asis@itemP. L'Ecuyer, ``Maximally Equidistributed Combined TauswortheGenerators'', @cite{Mathematics of Computation}, 65, 213 (1996), 203--213.@end itemize@noindentThe generator @code{gsl_rng_taus2} uses the same algorithm as@code{gsl_rng_taus} but with an improved seeding procedure described inthe paper,@itemize @asis@itemP. L'Ecuyer, ``Tables of Maximally Equidistributed Combined LFSRGenerators'', @cite{Mathematics of Computation}, 68, 225 (1999), 261--269@end itemize@noindentThe 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 RegisterThe @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@exampler_n = r_@{n-A@} ^^ r_@{n-B@} ^^ r_@{n-C@} ^^ r_@{n-D@}@end example@end ifinfoZiff (ref below) notes that ``it is now widely known'' that two-tapregisters (such as R250, which is described below)have serious flaws, the most obvious one being the three-pointcorrelation that comes from the definition of the generator.  Nicemathematical properties can be derived for GFSR's, and numerics bearsout the claim that 4-tap GFSR's with appropriately chosen offsets are asrandom as can be measured, using the author's test.This implementation uses the values suggested the example on p392 ofZiff'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 thisimplementation), then the sequence is said to be maximal; that meansthat 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 allzeros 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-bitinteger amounts to 32 parallel implementations of one-bitgenerators.  One consequence of this is that the period of this32-bit generator is the same as for the one-bit generator.Moreover, this independence means that all 32-bit patterns areequally likely, and in particular that 0 is an allowed randomvalue.  (We are grateful to Heiko Bauke for clarifying for us theseproperties of GFSR random number generators.)For more information see,@itemize @asis@item

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?