blitz Version 0.9
|
00001 /*************************************************************************** 00002 * blitz/rand-tt800.h Matsumoto and Kurita's TT800 uniform random 00003 * number generator. 00004 * 00005 * $Id: rand-tt800.h,v 1.2 2003/01/14 11:29:18 patricg Exp $ 00006 * 00007 *************************************************************************** 00008 * 00009 * The class TT800 encapsulates Makoto Matsumoto and Yoshiharu Kurita's 00010 * TT800 twisted generalized feedback shift register (TGFSR) random number 00011 * generator. The generator has period 2^800 - 1. 00012 * 00013 * Contact: M. Matsumoto <matumoto@math.keio.ac.jp> 00014 * 00015 * See: M. Matsumoto and Y. Kurita, Twisted GFSR Generators II, 00016 * ACM Transactions on Modelling and Computer Simulation, 00017 * Vol. 4, No. 3, 1994, pages 254-266. 00018 * 00019 * (c) 1994 Association for Computing Machinery. 00020 * 00021 * Distributed with consent of the authors. 00022 * 00023 ***************************************************************************/ 00024 00025 #ifndef BZ_RAND_TT800_H 00026 #define BZ_RAND_TT800_H 00027 00028 #ifndef BZ_RANDOM_H 00029 #include <blitz/random.h> 00030 #endif 00031 00032 BZ_NAMESPACE(blitz) 00033 00034 class TT800 { 00035 00036 public: 00037 typedef double T_numtype; 00038 00039 TT800(double low = 0.0, double high = 1.0, double = 0.0) 00040 : low_(low), length_(high-low) 00041 { 00042 // Initial 25 seeds 00043 x[0] = 0x95f24dab; x[1] = 0x0b685215; x[2] = 0xe76ccae7; 00044 x[3] = 0xaf3ec239; x[4] = 0x715fad23; x[5] = 0x24a590ad; 00045 x[6] = 0x69e4b5ef; x[7] = 0xbf456141; x[8] = 0x96bc1b7b; 00046 x[9] = 0xa7bdf825; x[10] = 0xc1de75b7; x[11] = 0x8858a9c9; 00047 x[12] = 0x2da87693; x[13] = 0xb657f9dd; x[14] = 0xffdc8a9f; 00048 x[15] = 0x8121da71; x[16] = 0x8b823ecb; x[17] = 0x885d05f5; 00049 x[18] = 0x4e20cd47; x[19] = 0x5a9ad5d9; x[20] = 0x512c0c03; 00050 x[21] = 0xea857ccd; x[22] = 0x4cc1d30f; x[23] = 0x8891a8a1; 00051 x[24] = 0xa6b7aadb; 00052 00053 // Magic vector 'a', don't change 00054 mag01[0] = 0; 00055 mag01[1] = 0x8ebfd028; 00056 00057 k = 0; 00058 00059 // f = 1/(2^32); 00060 00061 f = (1.0 / 65536) / 65536; 00062 } 00063 00064 void randomize() 00065 { 00066 BZ_NOT_IMPLEMENTED(); // NEEDS_WORK 00067 } 00068 00069 unsigned long randomUint32() 00070 { 00071 if (k==N) 00072 generate(); 00073 00074 unsigned long y = x[k]; 00075 y ^= (y << 7) & 0x2b5b2500; /* s and b, magic vectors */ 00076 y ^= (y << 15) & 0xdb8b0000; /* t and c, magic vectors */ 00077 y &= 0xffffffff; /* you may delete this line if word size = 32 */ 00078 00079 // the following line was added by Makoto Matsumoto in the 1996 version 00080 // to improve lower bit's corellation. 00081 // Delete this line to use the code published in 1994. 00082 00083 y ^= (y >> 16); /* added to the 1994 version */ 00084 k++; 00085 } 00086 00087 double random() 00088 { 00089 unsigned long y1 = randomUint32(); 00090 unsigned long y2 = randomUint32(); 00091 00092 return low_ + length_ * ((y1 * f) * y2 * f); 00093 } 00094 00095 protected: 00096 void generate() 00097 { 00098 /* generate N words at one time */ 00099 int kk; 00100 for (kk=0;kk<N-M;kk++) { 00101 x[kk] = x[kk+M] ^ (x[kk] >> 1) ^ mag01[x[kk] % 2]; 00102 } 00103 for (; kk<N;kk++) { 00104 x[kk] = x[kk+(M-N)] ^ (x[kk] >> 1) ^ mag01[x[kk] % 2]; 00105 } 00106 k=0; 00107 } 00108 00109 private: 00110 enum { N = 25, M = 7 }; 00111 00112 double low_, length_; 00113 double f; 00114 int k; 00115 unsigned long x[N]; 00116 unsigned long mag01[2]; 00117 }; 00118 00119 BZ_NAMESPACE_END 00120 00121 #endif // BZ_RAND_TT800_H 00122