4 This program is public domain and was written by William S. England
\r
5 (Oct 1988). It is based on an article by:
\r
7 Stephen K. Park and Keith W. Miller. RANDOM NUMBER GENERATORS:
\r
8 GOOD ONES ARE HARD TO FIND. Communications of the ACM,
\r
9 New York, NY.,October 1988 p.1192
\r
17 The following is a portable c program for generating random numbers.
\r
18 The modulus and multiplier have been extensively tested and should
\r
19 not be changed except by someone who is a professional Lehmer generator
\r
20 writer. THIS GENERATOR REPRESENTS THE MINIMUM STANDARD AGAINST WHICH
\r
21 OTHER GENERATORS SHOULD BE JUDGED. ("Quote from the referenced article's
\r
26 ** These are pre-calculated below to compensate for c
\r
27 ** compilers that may overflow when building the code.
\r
34 ** To build the generator with the original ACM
\r
35 ** article's numbers use -DORIGINAL_NUMBERS when compiling.
\r
37 ** Original_numbers are the original published m and q in the
\r
38 ** ACM article above. John Burton has furnished numbers for
\r
39 ** a reportedly better generator. The new numbers are now
\r
40 ** used in this program by default.
\r
43 #ifndef ORIGINAL_NUMBERS
\r
44 #define m (unsigned long)2147483647
\r
45 #define q (unsigned long)44488
\r
47 #define a (unsigned int)48271
\r
48 #define r (unsigned int)3399
\r
50 #define successfulltest 399268537
\r
53 #ifdef ORIGINAL_NUMBERS
\r
54 #define m (unsigned long)2147483647
\r
55 #define q (unsigned long)127773
\r
57 #define a (unsigned int)16807
\r
58 #define r (unsigned int)2836
\r
60 #define successfulltest 1043618065
\r
67 ** F(z) = G(z)+mT(z)
\r
68 ** G(z) = a(z%q)- r(z/q)
\r
69 ** T(z) = (z/q) - (az/m)
\r
71 ** F(z) = a(z%q)- rz/q+ m((z/q) - a(z/m))
\r
72 ** = a(z%q)- rz/q+ m(z/q) - az
\r
77 void srand( /* unsigned long*/ initial_seed)
\r
78 unsigned long initial_seed;
\r
80 seed = initial_seed;
\r
86 unsigned long rand(/*void*/){
\r
105 #include <stdio.h>
\r
107 ** The result of running this program should be
\r
108 ** "successfulltest". If this program does not yield this
\r
109 ** value then your compiler has not implemented this
\r
110 ** program correctly.
\r
112 ** To compile with test option under unix use; 'cc -DTESTRAND random.c'
\r
114 ** Be sure to compile without test option for use in applications.
\r
115 ** ( Now why did I have to say that ??? )
\r
128 for( i = 1; i <= 10001; i++){
\r
132 printf("Sequence %5i, Seed= %10i\n", i, seed );
\r
135 if( seed == successfulltest )
\r
140 printf("The random number generator works correctly.\n\n");
\r
143 printf("The random number generator DOES NOT WORK!\n\n");
\r