00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #ifndef MERSENNETWISTER_H
00044 #define MERSENNETWISTER_H
00045
00046
00047
00048
00049 #include <iostream>
00050 #include <limits.h>
00051 #include <stdio.h>
00052 #include <time.h>
00053
00054 class MTRand {
00055
00056 public:
00057 typedef unsigned long uint32;
00058
00059 enum { N = 624 };
00060 enum { SAVE = N + 1 };
00061
00062 protected:
00063 enum { M = 397 };
00064 enum { MAGIC = 0x9908b0dfU };
00065
00066 uint32 state[N];
00067 uint32 *pNext;
00068 int left;
00069
00070
00071
00072 public:
00073 MTRand( const uint32& oneSeed );
00074 MTRand( uint32 *const bigSeed );
00075 MTRand();
00076
00077
00078
00079
00080
00081 double rand();
00082 double rand( const double& n );
00083 double randExc();
00084 double randExc( const double& n );
00085 double randDblExc();
00086 double randDblExc( const double& n );
00087 uint32 randInt();
00088 uint32 randInt( const uint32& n );
00089 double operator()() { return rand(); }
00090
00091
00092 void seed( uint32 oneSeed );
00093 void seed( uint32 *const bigSeed );
00094 void seed();
00095
00096
00097 void save( uint32* saveArray ) const;
00098 void load( uint32 *const loadArray );
00099 friend std::ostream& operator<<( std::ostream& os, const MTRand& mtrand );
00100 friend std::istream& operator>>( std::istream& is, MTRand& mtrand );
00101
00102 protected:
00103 void reload();
00104 uint32 hiBit( const uint32& u ) const { return u & 0x80000000U; }
00105 uint32 loBit( const uint32& u ) const { return u & 0x00000001U; }
00106 uint32 loBits( const uint32& u ) const { return u & 0x7fffffffU; }
00107 uint32 mixBits( const uint32& u, const uint32& v ) const
00108 { return hiBit(u) | loBits(v); }
00109 uint32 twist( const uint32& m, const uint32& s0, const uint32& s1 ) const
00110 { return m ^ (mixBits(s0,s1)>>1) ^ (loBit(s1) ? MAGIC : 0U); }
00111 static uint32 hash( time_t t, clock_t c );
00112 };
00113
00114
00115 inline MTRand::MTRand( const uint32& oneSeed )
00116 { seed(oneSeed); }
00117
00118 inline MTRand::MTRand( uint32 *const bigSeed )
00119 { seed(bigSeed); }
00120
00121 inline MTRand::MTRand()
00122 { seed(); }
00123
00124 inline double MTRand::rand()
00125 { return double(randInt()) * 2.3283064370807974e-10; }
00126
00127 inline double MTRand::rand( const double& n )
00128 { return rand() * n; }
00129
00130 inline double MTRand::randExc()
00131 { return double(randInt()) * 2.3283064365386963e-10; }
00132
00133 inline double MTRand::randExc( const double& n )
00134 { return randExc() * n; }
00135
00136 inline double MTRand::randDblExc()
00137 { return double( 1.0 + randInt() ) * 2.3283064359965952e-10; }
00138
00139 inline double MTRand::randDblExc( const double& n )
00140 { return randDblExc() * n; }
00141
00142 inline MTRand::uint32 MTRand::randInt()
00143 {
00144 if( left == 0 ) reload();
00145 --left;
00146
00147 register uint32 s1;
00148 s1 = *pNext++;
00149 s1 ^= (s1 >> 11);
00150 s1 ^= (s1 << 7) & 0x9d2c5680U;
00151 s1 ^= (s1 << 15) & 0xefc60000U;
00152 return ( s1 ^ (s1 >> 18) );
00153 }
00154
00155
00156 inline MTRand::uint32 MTRand::randInt( const uint32& n )
00157 {
00158
00159 uint32 used = ~0;
00160 for( uint32 m = n; m; used <<= 1, m >>= 1 ) {}
00161 used = ~used;
00162
00163
00164 uint32 i;
00165 do
00166 i = randInt() & used;
00167 while( i > n );
00168 return i;
00169 }
00170
00171
00172 inline void MTRand::seed( uint32 oneSeed )
00173 {
00174
00175 register uint32 *s;
00176 register int i;
00177 for( i = N, s = state;
00178 i--;
00179 *s = oneSeed & 0xffff0000,
00180 *s++ |= ( (oneSeed *= 69069U)++ & 0xffff0000 ) >> 16,
00181 (oneSeed *= 69069U)++ ) {}
00182 reload();
00183 }
00184
00185
00186 inline void MTRand::seed( uint32 *const bigSeed )
00187 {
00188
00189
00190
00191
00192
00193
00194
00195 register uint32 *s = state, *b = bigSeed;
00196 register int i = N;
00197 for( ; i--; *s++ = *b++ & 0xffffffff ) {}
00198 reload();
00199 }
00200
00201
00202 inline void MTRand::seed()
00203 {
00204
00205
00206
00207
00208 FILE* urandom = fopen( "/dev/urandom", "rb" );
00209 if( urandom )
00210 {
00211 register uint32 *s = state;
00212 register int i = N;
00213 register bool success = true;
00214 while( success && i-- )
00215 {
00216 success = fread( s, sizeof(uint32), 1, urandom );
00217 *s++ &= 0xffffffff;
00218 }
00219 fclose(urandom);
00220 if( success )
00221 {
00222
00223
00224
00225 reload();
00226 return;
00227 }
00228 }
00229
00230
00231 seed( hash( time(NULL), clock() ) );
00232 }
00233
00234
00235 inline void MTRand::reload()
00236 {
00237
00238
00239 register uint32 *p = state;
00240 register int i;
00241 for( i = N - M; i--; ++p )
00242 *p = twist( p[M], p[0], p[1] );
00243 for( i = M; --i; ++p )
00244 *p = twist( p[M-N], p[0], p[1] );
00245 *p = twist( p[M-N], p[0], state[0] );
00246
00247 left = N, pNext = state;
00248 }
00249
00250
00251 inline MTRand::uint32 MTRand::hash( time_t t, clock_t c )
00252 {
00253
00254
00255
00256
00257 static uint32 differ = 0;
00258
00259 uint32 h1 = 0;
00260 unsigned char *p = (unsigned char *) &t;
00261 for( size_t i = 0; i < sizeof(t); ++i )
00262 {
00263 h1 *= UCHAR_MAX + 2U;
00264 h1 += p[i];
00265 }
00266 uint32 h2 = 0;
00267 p = (unsigned char *) &c;
00268 for( size_t j = 0; j < sizeof(c); ++j )
00269 {
00270 h2 *= UCHAR_MAX + 2U;
00271 h2 += p[j];
00272 }
00273 return ( h1 + differ++ ) ^ h2;
00274 }
00275
00276
00277 inline void MTRand::save( uint32* saveArray ) const
00278 {
00279 register uint32 *sa = saveArray;
00280 register const uint32 *s = state;
00281 register int i = N;
00282 for( ; i--; *sa++ = *s++ ) {}
00283 *sa = left;
00284 }
00285
00286
00287 inline void MTRand::load( uint32 *const loadArray )
00288 {
00289 register uint32 *s = state;
00290 register uint32 *la = loadArray;
00291 register int i = N;
00292 for( ; i--; *s++ = *la++ ) {}
00293 left = *la;
00294 pNext = &state[N-left];
00295 }
00296
00297
00298 inline std::ostream& operator<<( std::ostream& os, const MTRand& mtrand )
00299 {
00300 register const MTRand::uint32 *s = mtrand.state;
00301 register int i = mtrand.N;
00302 for( ; i--; os << *s++ << "\t" ) {}
00303 return os << mtrand.left;
00304 }
00305
00306
00307 inline std::istream& operator>>( std::istream& is, MTRand& mtrand )
00308 {
00309 register MTRand::uint32 *s = mtrand.state;
00310 register int i = mtrand.N;
00311 for( ; i--; is >> *s++ ) {}
00312 is >> mtrand.left;
00313 mtrand.pNext = &mtrand.state[mtrand.N-mtrand.left];
00314 return is;
00315 }
00316
00317 #endif //MERSENNETWISTER_H
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347