#include "MusimatChapter9.h"Go to the source code of this file.
Functions | |
| MusimatChapter9Section (C090703) | |
| Integer | LCRandom () |
| Static Void | para1 () |
Variables | |
| Const Integer | a = 16807 |
| Const Integer | b = 0 |
| Const Integer | c = 2147483647 |
| Integer | x = 1 |
| Integer LCRandom | ( | ) |
| MusimatChapter9Section | ( | C090703 | ) |
Definition at line 2 of file C090703.cpp.
References para1().
00002 { 00003 Print("*** 9.7.3 Linear Congruential Method ***"); 00004 /***************************************************************************** 00005 00006 9.7.3 Linear Congruential Method 00007 00008 Equation (9.1) shows the linear congruential method for generating random numbers, 00009 introduced by D. H. Lehmer in 1948 (Knuth 1973, vol. 2): 00010 00011 x[n+1] = ((ax[n]+b))c, n>=0. (9.1) 00012 00013 The notation ((x))n means “x is reduced modulo n.” The result is the remainder 00014 after integer division of x by n (see appendix A). 00015 Equation (9.1) is a recurrence relation because the result of the previous step 00016 (x[n]) is used to calculate a subsequent step (x[n+1]). It is linear because the 00017 ax + b part of the equation describes a straight line that intersects the y-axis 00018 at offset b with slope a. Congruence is a condition of equivalence between two 00019 integers modulo some other integer, and refers here simply to the fact that 00020 modulo arithmetic is being used. 00021 00022 For successive computations of x, the output will grow until it reaches the value c. 00023 When c is exceeded, the new value of x is effectively reset to x – c by the modulus operation. 00024 A new slope will grow from this point, and this process repeats endlessly. 00025 The result can be quite predictable depending upon the values of a, b, c, and x[0], 00026 the initial value of x. 00027 00028 For instance, if a = b = x[0] = 1, and c = infinity, an ascending straight line at a 45 degree 00029 slope is produced. However, for other values, the numbers generated can appear random. 00030 In practice, the modulus c should be as large as possible in order to produce long 00031 random sequences. On a computer, the ultimate limit of c is the arithmetic precision 00032 of that machine. For example, if the computer uses 16-bit arithmetic, random numbers 00033 generated by this method can have at most a period of 2^16 = 65,536 values before 00034 the pattern repeats. 00035 00036 The quality of randomness within a period varies depending on the values chosen 00037 for a, x, and b. Much heavy-duty mathematics has been expended choosing good 00038 values (Knuth 1973, vol. 2). For 32-bit arithmetic, Park and Miller (1988) recommend 00039 a = 16,807, b = 0, and c = 2,147,483,647. 00040 The linear congruential method is appealing because once a good set of the parameters is 00041 found, it is very easy to program on a computer. The LCRandom() method returns a random 00042 number by the linear congruential method each time it is called: 00043 *****************************************************************************/ 00044 para1(); // Step into this function to continue. 00045 }
| Static Void para1 | ( | ) |
Definition at line 62 of file C090703.cpp.
References LCRandom(), and x.
00062 { 00063 /***************************************************************************** 00064 The parameters a, b, and c are constant (time-invariant) system parameters. 00065 Parameter x is initialized in this example to 1, but it can be initialized 00066 to any other integer. The value of a * x + b is calculated, the remainder 00067 is found modulo c, and the result is reassigned to x. 00068 00069 While the value of x is less than c, x grows linearly. When the expression 00070 a * x + b eventually produces a value beyond the range of c, then x is reduced 00071 modulo c. The random effect of this method comes from the surprisingly unpredictable 00072 sequence of remainders generated by the modulus operation, depending upon careful 00073 choice of parameters. 00074 00075 The calculation of x ranges over all possible positive and negative integers 00076 smaller than the value of ±c. But it is generally preferable to constrain its 00077 choices to a range. To make this conversion easier, we force the result to be a 00078 positive integer. 00079 00080 Below is a sample invocation of LCRandom(). 00081 *****************************************************************************/ 00082 Print("*** Ten invocations of LCRandom() ***"); 00083 IntegerList x; 00084 00085 For (Integer i = 0; i < 10; i++ ) 00086 x[i] = LCRandom(); 00087 00088 Print( x ); 00089 }}
| Const Integer a = 16807 |
Definition at line 48 of file C090703.cpp.
| Const Integer b = 0 |
Definition at line 49 of file C090703.cpp.
| Const Integer c = 2147483647 |
Definition at line 50 of file C090703.cpp.
| Integer x = 1 |
Definition at line 51 of file C090703.cpp.
1.4.7