00001 #include "MusimatChapter9.h" 00002 MusimatChapter9Section(C090703) { 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 } 00046 00047 //Constants from Park and Miller 00048 Const Integer a = 16807; // a, b, c and x are global constant values 00049 Const Integer b = 0; 00050 Const Integer c = 2147483647; 00051 Integer x = 1; // x stores the value produced by 00052 // LCRandom between invocations 00053 Integer LCRandom(){ 00054 x = Mod(a * x + b, c); // update x based on its previous value 00055 Integer r = x; // x may be positive or negative 00056 If (r < 0) // force the result to be positive 00057 r = -r; 00058 Return(r); 00059 } 00060 00061 00062 Static Void para1() { 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 }} 00090 00092 /* $Revision: 1.4 $ $Date: 2006/09/12 17:37:24 $ $Author: dgl $ $Name: $ $Id: _c090703_8cpp-source.html,v 1.4 2006/09/12 17:37:24 dgl Exp $ */ 00093 // The Musimat Tutorial © 2006 Gareth Loy 00094 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 00095 // and published exclusively by The MIT Press. 00096 // This program is released WITHOUT ANY WARRANTY; without even the implied 00097 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00098 // For information on usage and redistribution, and for a DISCLAIMER OF ALL 00099 // WARRANTIES, see the file, "LICENSE.txt," in this distribution. 00100 // "Musimathics" is available here: http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916 00101 // Gareth Loy's Musimathics website: http://www.musimathics.com/ 00102 // The Musimat website: http://www.musimat.com/ 00103 // This program is released under the terms of the GNU General Public License 00104 // available here: http://www.gnu.org/licenses/gpl.txt
1.4.7