正在加载图片...
7.1 Uniform Deviates 279 #define IA 16807 #define IM 2147483647 #define AM (1.0/IM) #define IQ 127773 #define IR 2836 #define MASK 123459876 float rano(long *idum) "Minimal"random number generator of Park and Miller.Returns a uniform random deviate between 0.0 and 1.0.Set or reset idum to any integer value (except the unlikely value MASK) to initialize the sequence;idum must not be altered between calls for successive deviates in a sequence. long k; float ans; g *idum“=MASK; XORing with MASK allows use of zero and other k=(*idum)/IQ; simple bit patterns for idum. *idum=IA*(*idum-k*IQ)-IR*k: Compute idum=(IA*idum)%IM without over- if (*idum O)*idum +IM; flows by Schrage's method. ans=AM*(*idum) Convert idum to a floating result. *idum MASK; Unmask before return. RECIPES return ans; 婆是 Press. The period of rano is 231-22.1 x 109.A peculiarity of generators of the form (7.1.2)is that the value 0 must never be allowed as the initial seed-it perpetuates itself-and it never occurs for any nonzero initial seed.Experience 9 has shown that users always manage to call random number generators with the seed idum=0.That is why rano performs its exclusive-or with an arbitrary constant both SCIENTIFIC on entry and exit.If you are the first user in history to be proofagainst human error, you can remove the two lines with the A operation. 6 Park and Miller discuss two other multipliers a that can be used with the same m =231-1.These are a =48271 (with g=44488 and r =3399)and a 69621 (with g=30845 and r=23902).These can be substituted in the routine rano if desired;they may be slightly superior to Lewis et al.'s longer-tested values.No values other than these should be used. The routine rano is a Minimal Standard,satisfactory for the majority of Recipes Numerica 10621 applications,but we do not recommend it as the final word on random number 43106 generators.Our reason is precisely the simplicity of the Minimal Standard.It is Recipes not hard to think of situations where successive random numbers might be used in a way that accidentally conflicts with the generation algorithm.For example, since successive numbers differ by a multiple of only 1.6 x 104 out of a modulus of North Software. more than 2 x 109,very small random numbers will tend to be followed by smaller than average values.One time in 105,for example,there will be a value<10-6 returned (as there should be),but this will ahways be followed by a value less than about 0.0168.One can easily think of applications involving rare events where this property would lead to wrong results. There are other,more subtle,serial correlations present in rano.For example, if successive points (Ii,I+1)are binned into a two-dimensional plane for i= 1,2,...,N,then the resulting distribution fails the x2test when N is greater than a few x107,much less than the period m-2.Since low-order serial correlations have historically been such a bugaboo,and since there is a very simple way to remove7.1 Uniform Deviates 279 Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copyin Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5) g of machine￾readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America). #define IA 16807 #define IM 2147483647 #define AM (1.0/IM) #define IQ 127773 #define IR 2836 #define MASK 123459876 float ran0(long *idum) “Minimal” random number generator of Park and Miller. Returns a uniform random deviate between 0.0 and 1.0. Set or reset idum to any integer value (except the unlikely value MASK) to initialize the sequence; idum must not be altered between calls for successive deviates in a sequence. { long k; float ans; *idum ^= MASK; XORing with MASK allows use of zero and other k=(*idum)/IQ; simple bit patterns for idum. *idum=IA*(*idum-k*IQ)-IR*k; Compute idum=(IA*idum) % IM without over￾if (*idum < 0) *idum += IM; flows by Schrage’s method. ans=AM*(*idum); Convert idum to a floating result. *idum ^= MASK; Unmask before return. return ans; } The period of ran0 is 231 − 2 ≈ 2.1 × 109. A peculiarity of generators of the form (7.1.2) is that the value 0 must never be allowed as the initial seed — it perpetuates itself — and it never occurs for any nonzero initial seed. Experience has shown that users always manage to call random number generators with the seed idum=0. That is why ran0 performs its exclusive-or with an arbitrary constant both on entry and exit. If you are the first user in history to be proof against human error, you can remove the two lines with the ∧ operation. Park and Miller discuss two other multipliers a that can be used with the same m = 231 − 1. These are a = 48271 (with q = 44488 and r = 3399) and a = 69621 (with q = 30845 and r = 23902). These can be substituted in the routine ran0 if desired; they may be slightly superior to Lewis et al.’s longer-tested values. No values other than these should be used. The routine ran0 is a Minimal Standard, satisfactory for the majority of applications, but we do not recommend it as the final word on random number generators. Our reason is precisely the simplicity of the Minimal Standard. It is not hard to think of situations where successive random numbers might be used in a way that accidentally conflicts with the generation algorithm. For example, since successive numbers differ by a multiple of only 1.6 × 10 4 out of a modulus of more than 2 × 109, very small random numbers will tend to be followed by smaller than average values. One time in 106, for example, there will be a value < 10−6 returned (as there should be), but this will always be followed by a value less than about 0.0168. One can easily think of applications involving rare events where this property would lead to wrong results. There are other, more subtle, serial correlations present in ran0. For example, if successive points (Ii, Ii+1) are binned into a two-dimensional plane for i = 1, 2,...,N, then the resulting distribution fails the χ2 test when N is greater than a few ×107, much less than the period m − 2. Since low-order serial correlations have historically been such a bugaboo, and since there is a very simple way to remove
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有