c7-5 (779519), страница 2
Текст из файла (страница 2)
This is no more than a convenient way of defining many different sequences(negative values of idum), but still with random access to each sequence (positive values ofidum). For getting a floating-point number from the 32-bit integer, we like to do it by themasking trick described at the end of §7.1, above. The hex constants 3F800000 and 007FFFFFare the appropriate ones for computers using the IEEE representation for 32-bit floating-pointnumbers (e.g., IBM PCs and most UNIX workstations). For DEC VAXes, the correct hexconstants are, respectively, 00004080 and FFFF007F. For greater portability, you can insteadconstruct a floating number by making the (signed) 32-bit integer nonnegative (typically, youadd exactly 231 if it is negative) and then multiplying it by a floating constant (typically 2.−31 ).An interesting, and sometimes useful, feature of the routine ran4, below, is that it allowsrandom access to the nth random value in a sequence, without the necessity of first generatingvalues 1 · · · n − 1.
This property is shared by any random number generator based on hashing(the technique of mapping data keys, which may be highly clustered in value, approximatelyuniformly into a storage address space) [5,6] . One might have a simulation problem in whichsome certain rare situation becomes recognizable by its consequences only considerably afterit has occurred. One may wish to restart the simulation back at that occurrence, using identicalrandom values but, say, varying some other control parameters.
The relevant question mightthen be something like “what random numbers were used in cycle number 337098901?” Itmight already be cycle number 395100273 before the question comes up. Random generatorsbased on recursion, rather than hashing, cannot easily answer such a question.float ran4(long *idum)Returns a uniform random deviate in the range 0.0 to 1.0, generated by pseudo-DES (DESlike) hashing of the 64-bit word (idums,idum), where idums was set by a previous call withnegative idum. Also increments idum.
Routine can be used to generate a random sequenceby successive calls, leaving idum unaltered between calls; or it can randomly access the nthdeviate in a sequence by calling with idum = n. Different sequences are initialized by calls withdiffering negative values of idum.{void psdes(unsigned long *lword, unsigned long *irword);unsigned long irword,itemp,lword;static long idums = 0;The hexadecimal constants jflone and jflmsk below are used to produce a floating numberbetween 1. and 2. by bitwise masking.
They are machine-dependent. See text.#if defined(vax) || defined(_vax_) || defined(__vax__) || defined(VAX)static unsigned long jflone = 0x00004080;static unsigned long jflmsk = 0xffff007f;#elsestatic unsigned long jflone = 0x3f800000;static unsigned long jflmsk = 0x007fffff;#endifif (*idum < 0) {idums = -(*idum);*idum=1;}irword=(*idum);lword=idums;Reset idums and prepare to return the firstdeviate in its sequence.Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)Copyright (C) 1988-1992 by Cambridge University Press.Programs Copyright (C) 1988-1992 by Numerical Recipes Software.Permission is granted for internet users to make one paper copy for their own personal use.
Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMsvisit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).}}304Chapter 7.psdes(&lword,&irword);itemp=jflone | (jflmsk & irword);++(*idum);return (*(float *)&itemp)-1.0;Random Numbers“Pseudo-DES” encode the words.Mask to a floating number between 1 and2.Subtraction moves range to 0.
to 1.}Values for Verifying the Implementation of psdesidum–1before psdes callafter psdes call (hex)ran4(idum)lwordirwordlwordirwordVAXPC11604D1DCE509C0C230.2758980.21912099199D97F8571A66CB41A0.2082040.849246–999917822309D643009840.0343070.375290999999D7F376F059BA89EB0.8386760.457334Successive calls to psdes with arguments −1, 99, −99, and 1, should produce exactly thelword and irword values shown. Masking conversion to a returned floating random valueis allowed to be machine dependent; values for VAX and PC are shown.CITED REFERENCES AND FURTHER READING:Data Encryption Standard, 1977 January 15, Federal Information Processing Standards Publication, number 46 (Washington: U.S. Department of Commerce, National Bureau of Standards).
[1]Guidelines for Implementing and Using the NBS Data Encryption Standard, 1981 April 1, FederalInformation Processing Standards Publication, number 74 (Washington: U.S. Departmentof Commerce, National Bureau of Standards). [2]Validating the Correctness of Hardware Implementations of the NBS Data Encryption Standard,1980, NBS Special Publication 500–20 (Washington: U.S. Department of Commerce, National Bureau of Standards). [3]Meyer, C.H. and Matyas, S.M.
1982, Cryptography: A New Dimension in Computer Data Security(New York: Wiley). [4]Knuth, D.E. 1973, Sorting and Searching, vol. 3 of The Art of Computer Programming (Reading,MA: Addison-Wesley), Chapter 6. [5]Vitter, J.S., and Chen, W-C. 1987, Design and Analysis of Coalesced Hashing (New York:Oxford University Press). [6]7.6 Simple Monte Carlo IntegrationInspirations for numerical methods can spring from unlikely sources. “Splines”first were flexible strips of wood used by draftsmen. “Simulated annealing” (weshall see in §10.9) is rooted in a thermodynamic analogy. And who does not feel atleast a faint echo of glamor in the name “Monte Carlo method”?Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)Copyright (C) 1988-1992 by Cambridge University Press.Programs Copyright (C) 1988-1992 by Numerical Recipes Software.Permission is granted for internet users to make one paper copy for their own personal use.
Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMsvisit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).The accompanying table gives data for verifying that ran4 and psdes work correctlyon your machine.
We do not advise the use of ran4 unless you are able to reproduce thehex values shown. Typically, ran4 is about 4 times slower than ran0 (§7.1), or about 3times slower than ran1..















