Random Numbers part 5
lượt xem 3
download
Random Numbers part 5
if (n != nold) { If n has changed, then compute useful quantien=n; ties. oldg=gammln(en+1.0); nold=n; } if (p != pold) { If p has changed, then compute useful quantipc=1.0p; ties. plog=log(p); pclog=log(pc); pold=p; } sq=sqrt(2.0*am*pc)
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Random Numbers part 5
 296 Chapter 7. Random Numbers if (n != nold) { If n has changed, then compute useful quanti en=n; ties. oldg=gammln(en+1.0); nold=n; } if (p != pold) { If p has changed, then compute useful quanti pc=1.0p; ties. plog=log(p); pclog=log(pc); visit website http://www.nr.com or call 18008727423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America). readable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine Copyright (C) 19881992 by Cambridge University Press.Programs Copyright (C) 19881992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0521431085) pold=p; } sq=sqrt(2.0*am*pc); The following code should by now seem familiar: do { rejection method with a Lorentzian compar do { ison function. angle=PI*ran1(idum); y=tan(angle); em=sq*y+am; } while (em < 0.0  em >= (en+1.0)); Reject. em=floor(em); Trick for integervalued distribution. t=1.2*sq*(1.0+y*y)*exp(oldggammln(em+1.0) gammln(enem+1.0)+em*plog+(enem)*pclog); } while (ran1(idum) > t); Reject. This happens about 1.5 times per devi bnl=em; ate, on average. } if (p != pp) bnl=nbnl; Remember to undo the symmetry transforma return bnl; tion. } See Devroye [2] and Bratley [3] for many additional algorithms. CITED REFERENCES AND FURTHER READING: Knuth, D.E. 1981, Seminumerical Algorithms, 2nd ed., vol. 2 of The Art of Computer Programming (Reading, MA: AddisonWesley), pp. 120ff. [1] Devroye, L. 1986, NonUniform Random Variate Generation (New York: SpringerVerlag), §X.4. [2] Bratley, P., Fox, B.L., and Schrage, E.L. 1983, A Guide to Simulation (New York: Springer Verlag). [3]. 7.4 Generation of Random Bits The C language gives you useful access to some machinelevel bitwise operations such as
 7.4 Generation of Random Bits 297 sufﬁce it to say that there are special polynomials among those whose coefﬁcients are zero or one. An example is x18 + x5 + x2 + x1 + x0 (7.4.1) which we can abbreviate by just writing the nonzero powers of x, e.g., visit website http://www.nr.com or call 18008727423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America). readable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine Copyright (C) 19881992 by Cambridge University Press.Programs Copyright (C) 19881992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0521431085) (18, 5, 2, 1, 0) Every primitive polynomial modulo 2 of order n (=18 above) deﬁnes a recurrence relation for obtaining a new random bit from the n preceding ones. The recurrence relation is guaranteed to produce a sequence of maximal length, i.e., cycle through all possible sequences of n bits (except all zeros) before it repeats. Therefore one can seed the sequence with any initial bit pattern (except all zeros), and get 2n − 1 random bits before the sequence repeats. Let the bits be numbered from 1 (most recently generated) through n (generated n steps ago), and denoted a1 , a2 , . . . , an . We want to give a formula for a new bit a0 . After generating a0 we will shift all the bits by one, so that the old an is ﬁnally lost, and the new a0 becomes a1 . We then apply the formula again, and so on. “Method I” is the easiest to implement in hardware, requiring only a single shift register n bits long and a few XOR (“exclusive or” or bit addition mod 2) gates, the operation denoted in C by “∧”. For the primitive polynomial given above, the recurrence formula is a0 = a18 ∧ a5 ∧ a2 ∧ a1 (7.4.2) The terms that are ∧’d together can be thought of as “taps” on the shift register, ∧’d into the register’s input. More generally, there is precisely one term for each nonzero coefﬁcient in the primitive polynomial except the constant (zero bit) term. So the ﬁrst term will always be an for a primitive polynomial of degree n, while the last term might or might not be a1 , depending on whether the primitive polynomial has a term in x1 . While it is simple in hardware, Method I is somewhat cumbersome in C, because the individual bits must be collected by a sequence of fullword masks: int irbit1(unsigned long *iseed) Returns as an integer a random bit, based on the 18 lowsigniﬁcance bits in iseed (which is modiﬁed for the next call). { unsigned long newbit; The accumulated XOR’s. newbit = (*iseed >> 17) & 1 Get bit 18. ^ (*iseed >> 4) & 1 XOR with bit 5. ^ (*iseed >> 1) & 1 XOR with bit 2. ^ (*iseed & 1); XOR with bit 1. *iseed=(*iseed
 298 Chapter 7. Random Numbers 18 17 5 4 3 2 1 0 shift left (a) visit website http://www.nr.com or call 18008727423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America). readable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine Copyright (C) 19881992 by Cambridge University Press.Programs Copyright (C) 19881992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0521431085) 18 17 5 4 3 2 1 0 shift left (b) Figure 7.4.1. Two related methods for obtaining random bits from a shift register and a primitive polynomial modulo 2. (a) The contents of selected taps are combined by exclusiveor (addition modulo 2), and the result is shifted in from the right. This method is easiest to implement in hardware. (b) Selected bits are modiﬁed by exclusiveor with the leftmost bit, which is then shifted in from the right. This method is easiest to implement in software. “Method II” is less suited to direct hardware implementation (though still possible), but is beautifully suited to C. It modiﬁes more than one bit among the saved n bits as each new bit is generated (Figure 7.4.1). It generates the maximal length sequence, but not in the same order as Method I. The prescription for the primitive polynomial (7.4.1) is: a0 = a18 a5 = a5 ∧ a0 (7.4.3) a2 = a2 ∧ a0 a1 = a1 ∧ a0 In general there will be an exclusiveor for each nonzero term in the primitive polynomial except 0 and n. The nice feature about Method II is that all the exclusiveor’s can usually be done as a single fullword exclusiveor operation: #define IB1 1 Powers of 2. #define IB2 2 #define IB5 16 #define IB18 131072 #define MASK (IB1+IB2+IB5) int irbit2(unsigned long *iseed) Returns as an integer a random bit, based on the 18 lowsigniﬁcance bits in iseed (which is modiﬁed for the next call). { if (*iseed & IB18) { Change all masked bits, shift, and put 1 into bit 1. *iseed=((*iseed ^ MASK)
 7.4 Generation of Random Bits 299 *iseed
 300 Chapter 7. Random Numbers random ﬂoatingpoint number. They are not very random for that purpose; see Knuth [1]. Examples of acceptable uses of these random bits are: (i) multiplying a signal randomly by ±1 at a rapid “chip rate,” so as to spread its spectrum uniformly (but recoverably) across some desired bandpass, or (ii) Monte Carlo exploration of a binary tree, where decisions as to whether to branch left or right are to be made randomly. visit website http://www.nr.com or call 18008727423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America). readable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine Copyright (C) 19881992 by Cambridge University Press.Programs Copyright (C) 19881992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0521431085) Now we do not want you to go through life thinking that there is something special about the primitive polynomial of degree 18 used in the above examples. (We chose 18 because 218 is small enough for you to verify our claims directly by numerical experiment.) The accompanying table [2] lists one primitive polynomial for each degree up to 100. (In fact there exist many such for each degree. For example, see §7.7 for a complete table up to degree 10.) CITED REFERENCES AND FURTHER READING: Knuth, D.E. 1981, Seminumerical Algorithms, 2nd ed., vol. 2 of The Art of Computer Programming (Reading, MA: AddisonWesley), pp. 29ff. [1] Horowitz, P., and Hill, W. 1989, The Art of Electronics, 2nd ed. (Cambridge: Cambridge University Press), §§9.32–9.37. Tausworthe, R.C. 1965, Mathematics of Computation, vol. 19, pp. 201–209. Watson, E.J. 1962, Mathematics of Computation, vol. 16, pp. 368–369. [2] 7.5 Random Sequences Based on Data Encryption In Numerical Recipes’ ﬁrst edition,we described how to use the Data Encryption Standard (DES) [13] for the generation of random numbers. Unfortunately, when implemented in software in a highlevel language like C, DES is very slow, so excruciatingly slow, in fact, that our previous implementation can be viewed as more mischievous than useful. Here we give a much faster and simpler algorithm which, though it may not be secure in the cryptographic sense, generates about equally good random numbers. DES, like its progenitor cryptographic system LUCIFER, is a socalled “block product cipher” [4]. It acts on 64 bits of input by iteratively applying (16 times, in fact) a kind of highly nonlinear bitmixing function. Figure 7.5.1 shows the ﬂow of information in DES during this mixing. The function g, which takes 32bits into 32bits, is called the “cipher function.” Meyer and Matyas [4] discuss the importance of the cipher function being nonlinear, as well as other design criteria. DES constructs its cipher function g from an intricate set of bit permutations and table lookups acting on short sequences of consecutive bits. Apparently, this function was chosen to be particularly strong cryptographically (or conceivably as some critics contend, to have an exquisitely subtle cryptographic ﬂaw!). For our purposes, a different function g that can be rapidly computed in a highlevel computer language is preferable. Such a function may weaken the algorithm cryptographically. Our purposes are not, however, cryptographic: We want to ﬁnd the fastest g, and smallest number of iterations of the mixing procedure in Figure 7.5.1, such that our output random sequence passes the standard tests that are customarily applied to random number generators. The resulting algorithm will not be DES, but rather a kind of “pseudoDES,” better suited to the purpose at hand. Following the criterion, mentioned above, that g should be nonlinear, we must give the integer multiply operation a prominent place in g. Because 64bit registers are not generally accessible in highlevel languages, we must conﬁne ourselves to multiplying 16bit
CÓ THỂ BẠN MUỐN DOWNLOAD

GIỚI THIỆU VỀ AUTOITLập Trình Trên AutoIT part 5
5 p  139  77

Visual Basic 6 Vovisoft part 5
5 p  51  15

Autolt part 5
1 p  37  10

Cracker Handbook 1.0 part 5
7 p  55  9

Tự học AutoIT part 5
9 p  52  9

HTML part 5
5 p  48  8

Software Engineering For Students: A Programming Approach Part 5
10 p  55  8

Programming HandBook part 5
6 p  66  7

Microsoft WSH and VBScript Programming for the Absolute Beginner Part 5
10 p  56  7

Practical prototype and scipt.aculo.us part 5
6 p  42  6

Random Numbers part 2
13 p  38  4

Random Numbers part 1
2 p  23  4

Random Numbers part 3
4 p  29  3

AutoIT Help part 5
5 p  43  3

Lập Trình C# all Chap "NUMERICAL RECIPES IN C" part 5
13 p  33  2

Random Numbers part 7
6 p  29  2

Random Numbers part 6
5 p  39  2