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
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 #ifndef MTRAND_H
00058 #define MTRAND_H
00059
00060
00061
00062
00063 #include <iostream>
00064 #include <climits>
00065 #include <cstdio>
00066 #include <ctime>
00067 #include <cmath>
00068
00069 class MTRand {
00070
00071 public:
00072 typedef unsigned long uint32;
00073
00074 enum { N = 624 };
00075 enum { SAVE = N + 1 };
00076
00077 protected:
00078 enum { M = 397 };
00079
00080 uint32 state[N];
00081 uint32 *pNext;
00082 int left;
00083
00084
00085 public:
00086 MTRand( const uint32 oneSeed );
00087 MTRand( uint32 *const bigSeed, uint32 const seedLength = N );
00088 MTRand();
00089 MTRand( const MTRand& o );
00090
00091
00092
00093
00094
00095
00096 uint32 randInt();
00097 uint32 randInt( const uint32 n );
00098 double rand();
00099 double rand( const double n );
00100 double randExc();
00101 double randExc( const double n );
00102 double randDblExc();
00103 double randDblExc( const double n );
00104 double operator()();
00105
00106
00107 double rand53();
00108
00109
00110 double randNorm( const double mean = 0.0, const double stddev = 1.0 );
00111
00112
00113 void seed( const uint32 oneSeed );
00114 void seed( uint32 *const bigSeed, const uint32 seedLength = N );
00115 void seed();
00116
00117
00118 void save( uint32* saveArray ) const;
00119 void load( uint32 *const loadArray );
00120 friend std::ostream& operator<<( std::ostream& os, const MTRand& mtrand );
00121 friend std::istream& operator>>( std::istream& is, MTRand& mtrand );
00122 MTRand& operator=( const MTRand& o );
00123
00124 protected:
00125 void initialize( const uint32 oneSeed );
00126 void reload();
00127 uint32 hiBit( const uint32 u ) const { return u & 0x80000000UL; }
00128 uint32 loBit( const uint32 u ) const { return u & 0x00000001UL; }
00129 uint32 loBits( const uint32 u ) const { return u & 0x7fffffffUL; }
00130 uint32 mixBits( const uint32 u, const uint32 v ) const
00131 { return hiBit(u) | loBits(v); }
00132 uint32 magic( const uint32 u ) const
00133 { return loBit(u) ? 0x9908b0dfUL : 0x0UL; }
00134 uint32 twist( const uint32 m, const uint32 s0, const uint32 s1 ) const
00135 { return m ^ (mixBits(s0,s1)>>1) ^ magic(s1); }
00136 static uint32 hash( time_t t, clock_t c );
00137 };
00138
00139
00140
00141 inline MTRand::uint32 MTRand::hash( time_t t, clock_t c )
00142 {
00143
00144
00145
00146
00147 static uint32 differ = 0;
00148
00149 uint32 h1 = 0;
00150 unsigned char *p = (unsigned char *) &t;
00151 for( size_t i = 0; i < sizeof(t); ++i )
00152 {
00153 h1 *= UCHAR_MAX + 2U;
00154 h1 += p[i];
00155 }
00156 uint32 h2 = 0;
00157 p = (unsigned char *) &c;
00158 for( size_t j = 0; j < sizeof(c); ++j )
00159 {
00160 h2 *= UCHAR_MAX + 2U;
00161 h2 += p[j];
00162 }
00163 return ( h1 + differ++ ) ^ h2;
00164 }
00165
00166 inline void MTRand::initialize( const uint32 seed )
00167 {
00168
00169
00170
00171
00172 register uint32 *s = state;
00173 register uint32 *r = state;
00174 register int i = 1;
00175 *s++ = seed & 0xffffffffUL;
00176 for( ; i < N; ++i )
00177 {
00178 *s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL;
00179 r++;
00180 }
00181 }
00182
00183 inline void MTRand::reload()
00184 {
00185
00186
00187 static const int MmN = int(M) - int(N);
00188 register uint32 *p = state;
00189 register int i;
00190 for( i = N - M; i--; ++p )
00191 *p = twist( p[M], p[0], p[1] );
00192 for( i = M; --i; ++p )
00193 *p = twist( p[MmN], p[0], p[1] );
00194 *p = twist( p[MmN], p[0], state[0] );
00195
00196 left = N, pNext = state;
00197 }
00198
00199 inline void MTRand::seed( const uint32 oneSeed )
00200 {
00201
00202 initialize(oneSeed);
00203 reload();
00204 }
00205
00206 inline void MTRand::seed( uint32 *const bigSeed, const uint32 seedLength )
00207 {
00208
00209
00210
00211
00212
00213
00214 initialize(19650218UL);
00215 register int i = 1;
00216 register uint32 j = 0;
00217 register int k = ( N > seedLength ? N : seedLength );
00218 for( ; k; --k )
00219 {
00220 state[i] =
00221 state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1664525UL );
00222 state[i] += ( bigSeed[j] & 0xffffffffUL ) + j;
00223 state[i] &= 0xffffffffUL;
00224 ++i; ++j;
00225 if( i >= N ) { state[0] = state[N-1]; i = 1; }
00226 if( j >= seedLength ) j = 0;
00227 }
00228 for( k = N - 1; k; --k )
00229 {
00230 state[i] =
00231 state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1566083941UL );
00232 state[i] -= i;
00233 state[i] &= 0xffffffffUL;
00234 ++i;
00235 if( i >= N ) { state[0] = state[N-1]; i = 1; }
00236 }
00237 state[0] = 0x80000000UL;
00238 reload();
00239 }
00240
00241 inline void MTRand::seed()
00242 {
00243
00244
00245
00246
00247 FILE* urandom = fopen( "/dev/urandom", "rb" );
00248 if( urandom )
00249 {
00250 uint32 bigSeed[N];
00251 register uint32 *s = bigSeed;
00252 register int i = N;
00253 register bool success = true;
00254 while( success && i-- )
00255 success = fread( s++, sizeof(uint32), 1, urandom );
00256 fclose(urandom);
00257 if( success ) { seed( bigSeed, N ); return; }
00258 }
00259
00260
00261 seed( hash( time(NULL), clock() ) );
00262 }
00263
00264 inline MTRand::MTRand( const uint32 oneSeed )
00265 { seed(oneSeed); }
00266
00267 inline MTRand::MTRand( uint32 *const bigSeed, const uint32 seedLength )
00268 { seed(bigSeed,seedLength); }
00269
00270 inline MTRand::MTRand()
00271 { seed(); }
00272
00273 inline MTRand::MTRand( const MTRand& o )
00274 {
00275 register const uint32 *t = o.state;
00276 register uint32 *s = state;
00277 register int i = N;
00278 for( ; i--; *s++ = *t++ ) {}
00279 left = o.left;
00280 pNext = &state[N-left];
00281 }
00282
00283 inline MTRand::uint32 MTRand::randInt()
00284 {
00285
00286
00287
00288 if( left == 0 ) reload();
00289 --left;
00290
00291 register uint32 s1;
00292 s1 = *pNext++;
00293 s1 ^= (s1 >> 11);
00294 s1 ^= (s1 << 7) & 0x9d2c5680UL;
00295 s1 ^= (s1 << 15) & 0xefc60000UL;
00296 return ( s1 ^ (s1 >> 18) );
00297 }
00298
00299 inline MTRand::uint32 MTRand::randInt( const uint32 n )
00300 {
00301
00302
00303 uint32 used = n;
00304 used |= used >> 1;
00305 used |= used >> 2;
00306 used |= used >> 4;
00307 used |= used >> 8;
00308 used |= used >> 16;
00309
00310
00311 uint32 i;
00312 do
00313 i = randInt() & used;
00314 while( i > n );
00315 return i;
00316 }
00317
00318 inline double MTRand::rand()
00319 { return double(randInt()) * (1.0/4294967295.0); }
00320
00321 inline double MTRand::rand( const double n )
00322 { return rand() * n; }
00323
00324 inline double MTRand::randExc()
00325 { return double(randInt()) * (1.0/4294967296.0); }
00326
00327 inline double MTRand::randExc( const double n )
00328 { return randExc() * n; }
00329
00330 inline double MTRand::randDblExc()
00331 { return ( double(randInt()) + 0.5 ) * (1.0/4294967296.0); }
00332
00333 inline double MTRand::randDblExc( const double n )
00334 { return randDblExc() * n; }
00335
00336 inline double MTRand::rand53()
00337 {
00338 uint32 a = randInt() >> 5, b = randInt() >> 6;
00339 return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0);
00340 }
00341
00342 inline double MTRand::randNorm( const double mean, const double stddev )
00343 {
00344
00345
00346 double x, y, r;
00347 do
00348 {
00349 x = 2.0 * rand() - 1.0;
00350 y = 2.0 * rand() - 1.0;
00351 r = x * x + y * y;
00352 }
00353 while ( r >= 1.0 || r == 0.0 );
00354 double s = sqrt( -2.0 * log(r) / r );
00355 return mean + x * s * stddev;
00356 }
00357
00358 inline double MTRand::operator()()
00359 {
00360 return rand();
00361 }
00362
00363 inline void MTRand::save( uint32* saveArray ) const
00364 {
00365 register const uint32 *s = state;
00366 register uint32 *sa = saveArray;
00367 register int i = N;
00368 for( ; i--; *sa++ = *s++ ) {}
00369 *sa = left;
00370 }
00371
00372 inline void MTRand::load( uint32 *const loadArray )
00373 {
00374 register uint32 *s = state;
00375 register uint32 *la = loadArray;
00376 register int i = N;
00377 for( ; i--; *s++ = *la++ ) {}
00378 left = *la;
00379 pNext = &state[N-left];
00380 }
00381
00382 inline std::ostream& operator<<( std::ostream& os, const MTRand& mtrand )
00383 {
00384 register const MTRand::uint32 *s = mtrand.state;
00385 register int i = mtrand.N;
00386 for( ; i--; os << *s++ << "\t" ) {}
00387 return os << mtrand.left;
00388 }
00389
00390 inline std::istream& operator>>( std::istream& is, MTRand& mtrand )
00391 {
00392 register MTRand::uint32 *s = mtrand.state;
00393 register int i = mtrand.N;
00394 for( ; i--; is >> *s++ ) {}
00395 is >> mtrand.left;
00396 mtrand.pNext = &mtrand.state[mtrand.N-mtrand.left];
00397 return is;
00398 }
00399
00400 inline MTRand& MTRand::operator=( const MTRand& o )
00401 {
00402 if( this == &o ) return (*this);
00403 register const uint32 *t = o.state;
00404 register uint32 *s = state;
00405 register int i = N;
00406 for( ; i--; *s++ = *t++ ) {}
00407 left = o.left;
00408 pNext = &state[N-left];
00409 return (*this);
00410 }
00411
00412 #endif // MTRAND_H
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460