1 /***************************** RANDOMC.H *********************** 2001-10-24 AF *
2 *
3 * This file contains class declarations for the C++ library of uniform
4 * random number generators.
5 *
6 * Overview of classes:
7 * ====================
8 *
9 * class TRanrotBGenerator:
10 * Random number generator of type RANROT-B.
11 * Source file ranrotb.cpp
12 *
13 * class TRanrotWGenerator:
14 * Random number generator of type RANROT-W.
15 * Source file ranrotw.cpp
16 *
17 * class TRandomMotherOfAll:
18 * Random number generator of type Mother-of-All (Multiply with carry).
19 * Source file mother.cpp
20 *
21 * class TRandomMersenne:
22 * Random number generator of type Mersenne twister.
23 * Source file mersenne.cpp
24 *
25 * class TRandomMotRot:
26 * Combination of Mother-of-All and RANROT-W generators.
27 * Source file ranmoro.cpp and motrot.asm.
28 * Coded in assembly language for improved speed.
29 * Must link in RANDOMAO.LIB or RANDOMAC.LIB.
30 *
31 *
32 * Member functions (methods):
33 * ===========================
34 *
35 * All these classes have identical member functions:
36 *
37 * Constructor(long int seed):
38 * The seed can be any integer. Usually the time is used as seed.
39 * Executing a program twice with the same seed will give the same sequence of
40 * random numbers. A different seed will give a different sequence.
41 *
42 * double Random();
43 * Gives a floating point random number in the interval 0 <= x < 1.
44 * The resolution is 32 bits in TRanrotBGenerator, TRandomMotherOfAll and
45 * TRandomMersenne. 52 or 63 bits in TRanrotWGenerator. 63 bits in
46 * TRandomMotRot.
47 *
48 * int IRandom(int min, int max);
49 * Gives an integer random number in the interval min <= x <= max. (max-min < MAXINT).
50 * The resolution is the same as for Random().
51 *
52 * unsigned long BRandom();
53 * Gives 32 random bits.
54 * Only available in the classes TRanrotWGenerator and TRandomMersenne.
55 *
56 *
57 * Example:
58 * ========
59 * The file EX-RAN.CPP contains an example of how to generate random numbers.
60 *
61 *
62 * Further documentation:
63 * ======================
64 * The file randomc.htm contains further documentation on these random number
65 * generators.
66 *
67 * � 2002 Agner Fog. GNU General Public License www.gnu.org/copyleft/gpl.html
68 *******************************************************************************/
69 
70 #ifndef RANDOMC_H
71 #define RANDOMC_H
72 
73 #include <math.h>
74 #include <assert.h>
75 #include <stdio.h>
76 
77 class TRanrotBGenerator {              // encapsulate random number generator
78   enum constants {                     // define parameters
79     KK = 17, JJ = 10, R1 = 13, R2 =  9};
80   public:
81   void RandomInit(long int seed);      // initialization
82   int IRandom(int min, int max);       // get integer random number in desired interval
83   double Random();                     // get floating point random number
84   TRanrotBGenerator(long int seed);    // constructor
85   protected:
86   int p1, p2;                          // indexes into buffer
87   unsigned long randbuffer[KK];        // history buffer
88 #ifdef SELF_TEST
89   unsigned long randbufcopy[KK*2];     // used for self-test
90 #endif
91 };
92 
93 
94 #ifdef HIGH_RESOLUTION
95 typedef long double trfloat;           // use long double precision
96 #else
97 typedef double trfloat;                // use double precision
98 #endif
99 
100 class TRanrotWGenerator {              // encapsulate random number generator
101   enum constants {                     // define parameters
102     KK = 17, JJ = 10, R1 = 19, R2 =  27};
103   public:
104   void RandomInit(long int seed);      // initialization
105   int IRandom(int min, int max);       // get integer random number in desired interval
106   trfloat Random();                    // get floating point random number
107   unsigned long BRandom();             // output random bits
108   TRanrotWGenerator(long int seed);    // constructor
109   protected:
110   int p1, p2;                          // indexes into buffer
111   union {                              // used for conversion to float
112     trfloat randp1;
113     unsigned long randbits[3];};
114   unsigned long randbuffer[KK][2];     // history buffer
115 #ifdef SELF_TEST
116   unsigned long randbufcopy[KK*2][2];  // used for self-test
117 #endif
118 };
119 
120 class TRandomMotherOfAll {             // encapsulate random number generator
121   public:
122   void RandomInit(long int seed);      // initialization
123   int IRandom(int min, int max);       // get integer random number in desired interval
124   double Random();                     // get floating point random number
125   TRandomMotherOfAll(long int seed);   // constructor
126   protected:
127   double x[5];                         // history buffer
128   };
129 
130 class TRandomMersenne {                // encapsulate random number generator
131 #if 1
132     // define constants for MT11213A
133   enum constants {
134     N = 351, M = 175, R = 19,
135     TEMU = 11, TEMS = 7, TEMT = 15, TEML = 17};
136   // long constants cannot be defined as enum in 16-bit compilers:
137   #define MATRIX_A 0xE4BD75F5
138   #define TEMB     0x655E5280
139   #define TEMC     0xFFD58000
140 #else
141   enum constants {
142     // or constants for MT19937
143     N = 624, M = 397, R = 31,
144     TEMU = 11, TEMS = 7, TEMT = 15, TEML = 18};
145   #define MATRIX_A 0x9908B0DF
146   #define TEMB     0x9D2C5680
147   #define TEMC     0xEFC60000
148 #endif
149   public:
TRandomMersenne(long int seed)150   TRandomMersenne(long int seed) {     // constructor
151     RandomInit(seed);}
152   void RandomInit(long int seed);      // re-seed
153   long IRandom(long min, long max);    // output random integer
154   double Random();                     // output random float
155   unsigned long BRandom();             // output random bits
156   private:
157   unsigned long mt[N];                 // state vector
158   int mti;                             // index into mt
159   };
160 
161 class TRandomMotRot {                  // encapsulate random number generator from assembly library
162   public:
163   int IRandom(int min, int max);       // get integer random number in desired interval
164   double Random();                     // get floating point random number
165   TRandomMotRot(long int seed);        // constructor
166   };
167 
168 #endif
169