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