正在加载图片...
284 Chapter 7.Random Numbers process data from an experiment not always in exactly the same order,for example,so that the first output is more"typical"than might otherwise be the case. For this kind of application,all we really need is a list of"good"choices for m,a,and c in equation(7.1.1).If we don't need a period longer than 10 to 106,say,we can keep the value of(m-1)a+e small enough to avoid overflows that would otherwise mandate the extra complexity of Schrage's method (above).We can thus easily embed in our programs unsigned long jran,ia,ic,im; float ran; iran=(iraniatic)%im; ran=(float)jran (float)im; whenever we want a quick and dirty uniform deviate,or jran=(jran*iatic)%im; NUMERICAL j=jlo+((jhi-jlo+1)*jran)/im; whenever we want an integer between ilo and jhi,inclusive.(In both cases jran was once initialized to any seed value between 0 and im-1.) RECIPES I Be sure to remember,however,that when im is small,the kth root of it,which is the 令 number of planes in k-space,is even smaller!So a quick and dirty generator should never be used to select points in k-space with k>1. With these caveats,some"good"choices for the constants are given in the accompanying table.These constants(i)give a period of maximal length im,and,more important,(ii)pass 2 Knuth's "spectral test"for dimensions 2,3,4,5,and 6.The increment ic is a prime,close to the value (-v3)im;actually almost any value of ic that is relatively prime to im will do just as well,but there is some "lore"favoring this choice (see [41,p.84). IENTIFIC An Even Quicker Generator 61 In C,if you multiply two unsigned long int integers on a machine with a 32-bit long integer representation,the value returned is the low-order 32 bits of the true 64-bit product.If we now choose m =232,the "mod"in equation (7.1.1)is free,and we have simply 1j+1=alj+c (7.1.6) 3、 10621 Knuth suggests a =1664525 as a suitable multiplier for this value of m.H.W.Lewis Numerica has conducted extensive tests of this value of a with c=1013904223,which is a prime close 43126 to (v5-2)m.The resulting in-line generator (we will call it ranqd1)is simply E喜 unsigned long idum; 1dum=1664525L*1dum+1013904223L; North This is about as good as any 32-bit linear congruential generator,entirely adequate for many uses.And,with only a single multiply and add,it is very fast. To check whether your machine has the desired integer properties,see if you can generate the following sequence of 32-bit values (given here in hex):00000000,3C6EF35F, 47502932,D1CCF6E9,AAF95334,6252E503,9F2EC686,57FE6C2D,A3D95FA8,81FD- BEE7,94FOAF1A,CBF633B1. If you need floating-point values instead of 32-bit integers,and want to avoid a divide by floating-point 232,a dirty trick is to mask in an exponent that makes the value lie between I and 2,then subtract 1.0.The resulting in-line generator(call it ranqd2)will look something like284 Chapter 7. Random Numbers 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). process data from an experiment not always in exactly the same order, for example, so that the first output is more “typical” than might otherwise be the case. For this kind of application, all we really need is a list of “good” choices for m, a, and c in equation (7.1.1). If we don’t need a period longer than 104 to 106, say, we can keep the value of (m − 1)a + c small enough to avoid overflows that would otherwise mandate the extra complexity of Schrage’s method (above). We can thus easily embed in our programs unsigned long jran,ia,ic,im; float ran; ... jran=(jran*ia+ic) % im; ran=(float) jran / (float) im; whenever we want a quick and dirty uniform deviate, or jran=(jran*ia+ic) % im; j=jlo+((jhi-jlo+1)*jran)/im; whenever we want an integer between jlo and jhi, inclusive. (In both cases jran was once initialized to any seed value between 0 and im-1.) Be sure to remember, however, that when im is small, the kth root of it, which is the number of planes in k-space, is even smaller! So a quick and dirty generator should never be used to select points in k-space with k > 1. With these caveats, some “good” choices for the constants are given in the accompanying table. These constants (i) give a period of maximal length im, and, more important, (ii) pass Knuth’s “spectral test” for dimensions 2, 3, 4, 5, and 6. The increment ic is a prime, close to the value ( 1 2 − 1 6 √3)im; actually almost any value of ic that is relatively prime to im will do just as well, but there is some “lore” favoring this choice (see [4], p. 84). An Even Quicker Generator In C, if you multiply two unsigned long int integers on a machine with a 32-bit long integer representation, the value returned is the low-order 32 bits of the true 64-bit product. If we now choose m = 232, the “mod” in equation (7.1.1) is free, and we have simply Ij+1 = aIj + c (7.1.6) Knuth suggests a = 1664525 as a suitable multiplier for this value of m. H.W. Lewis has conducted extensive tests of this value of a with c = 1013904223, which is a prime close to ( √5 − 2)m. The resulting in-line generator (we will call it ranqd1) is simply unsigned long idum; ... idum = 1664525L*idum + 1013904223L; This is about as good as any 32-bit linear congruential generator, entirely adequate for many uses. And, with only a single multiply and add, it is very fast. To check whether your machine has the desired integer properties, see if you can generate the following sequence of 32-bit values (given here in hex): 00000000, 3C6EF35F, 47502932, D1CCF6E9, AAF95334, 6252E503, 9F2EC686, 57FE6C2D, A3D95FA8, 81FD￾BEE7, 94F0AF1A, CBF633B1. If you need floating-point values instead of 32-bit integers, and want to avoid a divide by floating-point 232, a dirty trick is to mask in an exponent that makes the value lie between 1 and 2, then subtract 1.0. The resulting in-line generator (call it ranqd2) will look something like
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有