正在加载图片...
7.1 Uniform Deviates 283 from the weaknesses,if any,of ran1 above.If you ever suspect trouble with one routine,it is a good idea to try the other in the same application.ran3 has one nice feature:if your machine is poor on integer arithmetic (i.e.,is limited to 16-bit integers),you can declare mj,mk,and ma[]as float,define mbig and mseed as 4000000 and 1618033,respectively,and the routine will be rendered entirely floating-point. #include <stdlib.h> Change to math.h in K&R C. #define MBIG 1000000000 #define MSEED 161803398 #define MZ 0 #define FAC (1.0/MBIG) According to Knuth,any large MBIG,and any smaller (but still large)MSEED can be substituted 83g (includi for the above values. 18881892 float ran3(long *idum) 11.800 Returns a uniform random deviate between 0.0 and 1.0.Set idum to any negative value to initialize or reinitialize the sequence. f from NUMERICAL RECIPESI static int inext,inextp; 6 static long ma[56]; The value 56 (range ma[1..55])is special and static int iff-0; should not be modified;see Knuth. long mj,mk; int i,ii,k; Press. if (*idum o ll iff ==0){ Initialization ART 1ff=1; mj=labs(MSEED-labs(*idum)); Initialize ma[55]using the seed idum and the 9 mj %MBIG: large number MSEED. Programs 豆 ma[55]-mj; mk=1; SCIENTIFIC for(1=1;1<=54;1++)[ Now initialize the rest of the table, 11=(21*1)%55; in a slightly random order, ma[ii]=mk: with numbers that are not especially random. mk=mj-mk; if (mk MZ)mk +MBIG; mj=ma [ii]; 1920 COMPUTING(ISBN for(k=1;k<=4;k++) We randomize them by "warming up the gener- for(1=1;1<=55;1+)[ ator." ma[1]-=ma[1+(1+30)%55]; if (ma[i]MZ)ma[i]+MBIG; inext=0; Prepare indices for our first generated number Numerical Recipes -43108 inextp=31; The constant 31 is special;see Knuth. *idum=1; (outside Here is where we start,except on initialization. Software. if (++inext =56)inext=1; Increment inext and inextp,wrapping around North if (++inextp ==56)inextp=1; 56to1. mj=ma[inext]-ma[inextp]; Generate a new random number subtractively. Ame if (mj MZ)mj +MBIG; Be sure that it is in range. ma[inext]=mj; Store it, return mj*FAC; and output the derived uniform deviate. Quick and Dirty Generators One sometimes would like a"quick and dirty"generator to embed in a program,perhaps taking only one or two lines of code,just to somewhat randomize things.One might wish to7.1 Uniform Deviates 283 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). from the weaknesses, if any, of ran1 above. If you ever suspect trouble with one routine, it is a good idea to try the other in the same application. ran3 has one nice feature: if your machine is poor on integer arithmetic (i.e., is limited to 16-bit integers), you can declare mj, mk, and ma[] as float, define mbig and mseed as 4000000 and 1618033, respectively, and the routine will be rendered entirely floating-point. #include <stdlib.h> Change to math.h in K&R C. #define MBIG 1000000000 #define MSEED 161803398 #define MZ 0 #define FAC (1.0/MBIG) According to Knuth, any large MBIG, and any smaller (but still large) MSEED can be substituted for the above values. float ran3(long *idum) Returns a uniform random deviate between 0.0 and 1.0. Set idum to any negative value to initialize or reinitialize the sequence. { static int inext,inextp; static long ma[56]; The value 56 (range ma[1..55]) is special and static int iff=0; should not be modified; see Knuth. long mj,mk; int i,ii,k; if (*idum < 0 || iff == 0) { Initialization. iff=1; mj=labs(MSEED-labs(*idum)); Initialize ma[55] using the seed idum and the mj %= MBIG; large number MSEED. ma[55]=mj; mk=1; for (i=1;i<=54;i++) { Now initialize the rest of the table, ii=(21*i) % 55; in a slightly random order, ma[ii]=mk; with numbers that are not especially random. mk=mj-mk; if (mk < MZ) mk += MBIG; mj=ma[ii]; } for (k=1;k<=4;k++) We randomize them by “warming upthe gener￾for (i=1;i<=55;i++) { ator.” ma[i] -= ma[1+(i+30) % 55]; if (ma[i] < MZ) ma[i] += MBIG; } inext=0; Prepare indices for our first generated number. inextp=31; The constant 31 is special; see Knuth. *idum=1; } Here is where we start, except on initialization. if (++inext == 56) inext=1; Increment inext and inextp, wrapping around if (++inextp == 56) inextp=1; 56 to 1. mj=ma[inext]-ma[inextp]; Generate a new random number subtractively. if (mj < MZ) mj += MBIG; Be sure that it is in range. ma[inext]=mj; Store it, return mj*FAC; and output the derived uniform deviate. } Quick and Dirty Generators One sometimes would like a “quick and dirty” generator to embed in a program, perhaps taking only one or two lines of code, just to somewhat randomize things. One might wish to
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有