| 1 | /*************************************************************************** | 
|---|
| 2 | * blitz/rand-tt800.h      Matsumoto and Kurita's TT800 uniform random | 
|---|
| 3 | *                         number generator. | 
|---|
| 4 | * | 
|---|
| 5 | * $Id: rand-tt800.h,v 1.1.1.1 1999-11-26 16:37:04 ansari Exp $ | 
|---|
| 6 | * | 
|---|
| 7 | *************************************************************************** | 
|---|
| 8 | * | 
|---|
| 9 | * The class TT800 encapsulates Makoto Matsumoto and Yoshiharu Kurita's | 
|---|
| 10 | * TT800 twisted generalized feedback shift register (TGFSR) random number | 
|---|
| 11 | * generator.  The generator has period 2^800 - 1. | 
|---|
| 12 | * | 
|---|
| 13 | * Contact: M. Matsumoto <matumoto@math.keio.ac.jp> | 
|---|
| 14 | * | 
|---|
| 15 | * See: M. Matsumoto and Y. Kurita, Twisted GFSR Generators II, | 
|---|
| 16 | *      ACM Transactions on Modelling and Computer Simulation, | 
|---|
| 17 | *      Vol. 4, No. 3, 1994, pages 254-266. | 
|---|
| 18 | * | 
|---|
| 19 | * (c) 1994 Association for Computing Machinery. | 
|---|
| 20 | * | 
|---|
| 21 | * Distributed with consent of the authors. | 
|---|
| 22 | * | 
|---|
| 23 | *************************************************************************** | 
|---|
| 24 | * | 
|---|
| 25 | * $Log: not supported by cvs2svn $ | 
|---|
| 26 | * Revision 1.1.1.1  1999/04/09  17:59:01  ansari | 
|---|
| 27 | * Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99 | 
|---|
| 28 | * | 
|---|
| 29 | * Revision 1.2  1997/07/16 14:51:20  tveldhui | 
|---|
| 30 | * Update: Alpha release 0.2 (Arrays) | 
|---|
| 31 | * | 
|---|
| 32 | * Revision 1.1  1997/02/28 13:39:51  tveldhui | 
|---|
| 33 | * Initial revision | 
|---|
| 34 | * | 
|---|
| 35 | */ | 
|---|
| 36 |  | 
|---|
| 37 | #ifndef BZ_RAND_TT800_H | 
|---|
| 38 | #define BZ_RAND_TT800_H | 
|---|
| 39 |  | 
|---|
| 40 | #ifndef BZ_RANDOM_H | 
|---|
| 41 | #include <blitz/random.h> | 
|---|
| 42 | #endif | 
|---|
| 43 |  | 
|---|
| 44 | BZ_NAMESPACE(blitz) | 
|---|
| 45 |  | 
|---|
| 46 | class TT800 { | 
|---|
| 47 |  | 
|---|
| 48 | public: | 
|---|
| 49 | typedef double T_numtype; | 
|---|
| 50 |  | 
|---|
| 51 | TT800(double low = 0.0, double high = 1.0, double = 0.0) | 
|---|
| 52 | : low_(low), length_(high-low) | 
|---|
| 53 | { | 
|---|
| 54 | // Initial 25 seeds | 
|---|
| 55 | x[0] = 0x95f24dab; x[1] = 0x0b685215; x[2] = 0xe76ccae7; | 
|---|
| 56 | x[3] = 0xaf3ec239; x[4] = 0x715fad23; x[5] = 0x24a590ad; | 
|---|
| 57 | x[6] = 0x69e4b5ef; x[7] = 0xbf456141; x[8] = 0x96bc1b7b; | 
|---|
| 58 | x[9] = 0xa7bdf825; x[10] = 0xc1de75b7; x[11] = 0x8858a9c9; | 
|---|
| 59 | x[12] = 0x2da87693; x[13] = 0xb657f9dd; x[14] = 0xffdc8a9f; | 
|---|
| 60 | x[15] = 0x8121da71; x[16] = 0x8b823ecb; x[17] = 0x885d05f5; | 
|---|
| 61 | x[18] = 0x4e20cd47; x[19] = 0x5a9ad5d9; x[20] = 0x512c0c03; | 
|---|
| 62 | x[21] = 0xea857ccd; x[22] = 0x4cc1d30f; x[23] = 0x8891a8a1; | 
|---|
| 63 | x[24] = 0xa6b7aadb; | 
|---|
| 64 |  | 
|---|
| 65 | // Magic vector 'a', don't change | 
|---|
| 66 | mag01[0] = 0; | 
|---|
| 67 | mag01[1] = 0x8ebfd028; | 
|---|
| 68 |  | 
|---|
| 69 | k = 0; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | void randomize() | 
|---|
| 73 | { | 
|---|
| 74 | BZ_NOT_IMPLEMENTED();            // NEEDS_WORK | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | double random() | 
|---|
| 78 | { | 
|---|
| 79 | if (k==N) | 
|---|
| 80 | generate(); | 
|---|
| 81 |  | 
|---|
| 82 | unsigned long y = x[k]; | 
|---|
| 83 | y ^= (y << 7) & 0x2b5b2500; /* s and b, magic vectors */ | 
|---|
| 84 | y ^= (y << 15) & 0xdb8b0000; /* t and c, magic vectors */ | 
|---|
| 85 | y &= 0xffffffff; /* you may delete this line if word size = 32 */ | 
|---|
| 86 |  | 
|---|
| 87 | // the following line was added by Makoto Matsumoto in the 1996 version | 
|---|
| 88 | // to improve lower bit's corellation. | 
|---|
| 89 | // Delete this line to use the code published in 1994. | 
|---|
| 90 |  | 
|---|
| 91 | y ^= (y >> 16); /* added to the 1994 version */ | 
|---|
| 92 | k++; | 
|---|
| 93 |  | 
|---|
| 94 | // NEEDS_WORK: this only generates a random number with | 
|---|
| 95 | // 15 bits of precision.  Need 48 bits for double. | 
|---|
| 96 | return low_ + length_ * ( (double) y / (unsigned long) 0xffffffff); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | protected: | 
|---|
| 100 | void generate() | 
|---|
| 101 | { | 
|---|
| 102 | /* generate N words at one time */ | 
|---|
| 103 | int kk; | 
|---|
| 104 | for (kk=0;kk<N-M;kk++) { | 
|---|
| 105 | x[kk] = x[kk+M] ^ (x[kk] >> 1) ^ mag01[x[kk] % 2]; | 
|---|
| 106 | } | 
|---|
| 107 | for (; kk<N;kk++) { | 
|---|
| 108 | x[kk] = x[kk+(M-N)] ^ (x[kk] >> 1) ^ mag01[x[kk] % 2]; | 
|---|
| 109 | } | 
|---|
| 110 | k=0; | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | private: | 
|---|
| 114 | enum { N = 25, M = 7 }; | 
|---|
| 115 |  | 
|---|
| 116 | double low_, length_; | 
|---|
| 117 | int k; | 
|---|
| 118 | unsigned long x[N]; | 
|---|
| 119 | unsigned long mag01[2]; | 
|---|
| 120 | }; | 
|---|
| 121 |  | 
|---|
| 122 | BZ_NAMESPACE_END | 
|---|
| 123 |  | 
|---|
| 124 | #endif // BZ_RAND_TT800_H | 
|---|
| 125 |  | 
|---|