#include <MersenneTwister.h>
Definition at line 74 of file MersenneTwister.h.
Public Types | |
| enum | { N = 624 } |
| enum | { SAVE = N + 1 } |
| typedef unsigned long | uint32 |
Public Member Functions | |
| void | load (uint32 *const loadArray) |
| MTRand () | |
| MTRand (uint32 *const bigSeed, uint32 const seedLength=N) | |
| MTRand (const uint32 &oneSeed) | |
| double | operator() () |
| double | rand (const double &n) |
| double | rand () |
| double | rand53 () |
| double | randDblExc (const double &n) |
| double | randDblExc () |
| double | randExc (const double &n) |
| double | randExc () |
| uint32 | randInt (const uint32 &n) |
| uint32 | randInt () |
| double | randNorm (const double &mean=0.0, const double &variance=0.0) |
| void | save (uint32 *saveArray) const |
| void | seed () |
| void | seed (uint32 *const bigSeed, const uint32 seedLength=N) |
| void | seed (const uint32 oneSeed) |
Protected Types | |
| enum | { M = 397 } |
Protected Member Functions | |
| uint32 | hiBit (const uint32 &u) const |
| void | initialize (const uint32 oneSeed) |
| uint32 | loBit (const uint32 &u) const |
| uint32 | loBits (const uint32 &u) const |
| uint32 | mixBits (const uint32 &u, const uint32 &v) const |
| void | reload () |
| uint32 | twist (const uint32 &m, const uint32 &s0, const uint32 &s1) const |
Static Protected Member Functions | |
| static uint32 | hash (time_t t, clock_t c) |
Protected Attributes | |
| int | left |
| uint32 * | pNext |
| uint32 | state [N] |
Friends | |
| std::ostream & | operator<< (std::ostream &os, const MTRand &mtrand) |
| std::istream & | operator>> (std::istream &is, MTRand &mtrand) |
| typedef unsigned long MTRand::uint32 |
Definition at line 77 of file MersenneTwister.h.
| anonymous enum |
| anonymous enum |
anonymous enum [protected] |
| MTRand::MTRand | ( | const uint32 & | oneSeed | ) | [inline] |
Definition at line 145 of file MersenneTwister.h.
References seed().
00146 { seed(bigSeed,seedLength); }
| MTRand::MTRand | ( | ) | [inline] |
| MTRand::uint32 MTRand::hash | ( | time_t | t, | |
| clock_t | c | |||
| ) | [inline, static, protected] |
Definition at line 323 of file MersenneTwister.h.
Referenced by seed().
00324 { 00325 // Get a uint32 from t and c 00326 // Better than uint32(x) in case x is floating point in [0,1] 00327 // Based on code by Lawrence Kirby (fred@genesis.demon.co.uk) 00328 00329 static uint32 differ = 0; // guarantee time-based seeds will change 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 }
| void MTRand::initialize | ( | const uint32 | oneSeed | ) | [inline, protected] |
Definition at line 289 of file MersenneTwister.h.
Referenced by seed().
00290 { 00291 // Initialize generator state with seed 00292 // See Knuth TAOCP Vol 2, 3rd Ed, p.106 for multiplier. 00293 // In previous versions, most significant bits (MSBs) of the seed affect 00294 // only MSBs of the state array. Modified 9 Jan 2002 by Makoto Matsumoto. 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 }
| void MTRand::load | ( | uint32 *const | loadArray | ) | [inline] |
| double MTRand::operator() | ( | ) | [inline] |
Definition at line 109 of file MersenneTwister.h.
References rand().
00109 { return rand(); } // same as rand()
| double MTRand::rand | ( | const double & | n | ) | [inline] |
| double MTRand::rand | ( | ) | [inline] |
Definition at line 151 of file MersenneTwister.h.
References randInt().
Referenced by operator()(), and rand().
00152 { return double(randInt()) * (1.0/4294967295.0); }
| double MTRand::rand53 | ( | ) | [inline] |
| double MTRand::randDblExc | ( | const double & | n | ) | [inline] |
Definition at line 166 of file MersenneTwister.h.
References randDblExc().
00167 { return randDblExc() * n; }
| double MTRand::randDblExc | ( | ) | [inline] |
Definition at line 163 of file MersenneTwister.h.
References randInt().
Referenced by randDblExc(), and randNorm().
00164 { return ( double(randInt()) + 0.5 ) * (1.0/4294967296.0); }
| double MTRand::randExc | ( | const double & | n | ) | [inline] |
Definition at line 160 of file MersenneTwister.h.
References randExc().
00161 { return randExc() * n; }
| double MTRand::randExc | ( | ) | [inline] |
Definition at line 157 of file MersenneTwister.h.
References randInt().
Referenced by RandHelper::rand(), randExc(), and randNorm().
00158 { return double(randInt()) * (1.0/4294967296.0); }
| MTRand::uint32 MTRand::randInt | ( | const uint32 & | n | ) | [inline] |
Definition at line 200 of file MersenneTwister.h.
References randInt().
00201 { 00202 // Find which bits are used in n 00203 // Optimized by Magnus Jonsson (magnus@smartelectronix.com) 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 // Draw numbers until one is found in [0,n] 00212 uint32 i; 00213 do 00214 i = randInt() & used; // toss unused bits to shorten search 00215 while( i > n ); 00216 return i; 00217 }
| MTRand::uint32 MTRand::randInt | ( | ) | [inline] |
Definition at line 184 of file MersenneTwister.h.
References left, pNext, and reload().
Referenced by RandHelper::rand(), rand(), rand53(), randDblExc(), randExc(), and randInt().
00185 { 00186 // Pull a 32-bit integer from the generator state 00187 // Every other access function simply transforms the numbers extracted here 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 }
| double MTRand::randNorm | ( | const double & | mean = 0.0, |
|
| const double & | variance = 0.0 | |||
| ) | [inline] |
Definition at line 175 of file MersenneTwister.h.
References randDblExc(), and randExc().
Referenced by RandHelper::randNorm().
00176 { 00177 // Return a real number from a normal (Gaussian) distribution with given 00178 // mean and variance by Box-Muller method 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 }
| void MTRand::reload | ( | ) | [inline, protected] |
Definition at line 307 of file MersenneTwister.h.
References left, M, N, pNext, state, and twist().
Referenced by randInt(), and seed().
00308 { 00309 // Generate N new values in state 00310 // Made clearer and faster by Matthew Bellew (matthew.bellew@home.com) 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 }
| void MTRand::save | ( | uint32 * | saveArray | ) | const [inline] |
| void MTRand::seed | ( | ) | [inline] |
Definition at line 265 of file MersenneTwister.h.
Referenced by MTRand().
00266 { 00267 // Seed the generator with an array from /dev/urandom if available 00268 // Otherwise use a hash of time() and clock() values 00269 00270 // First try getting an array from /dev/urandom 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; // !!! dk 16.02.2007 00280 fclose(urandom); 00281 if( success ) { seed( bigSeed, N ); return; } 00282 } 00283 00284 // Was not successful, so use time() and clock() instead 00285 seed( hash( time(NULL), clock() ) ); 00286 }
Definition at line 228 of file MersenneTwister.h.
References initialize(), N, reload(), and state.
00229 { 00230 // Seed the generator with an array of uint32's 00231 // There are 2^19937-1 possible initial states. This function allows 00232 // all of those to be accessed by providing at least 19937 bits (with a 00233 // default seed length of N = 624 uint32's). Any bits above the lower 32 00234 // in each element are discarded. 00235 // Just call seed() if you want to get array from /dev/urandom 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; // MSB is 1, assuring non-zero initial array 00261 reload(); 00262 }
| void MTRand::seed | ( | const uint32 | oneSeed | ) | [inline] |
Definition at line 220 of file MersenneTwister.h.
References initialize(), and reload().
Referenced by RandHelper::initRandGlobal().
00221 { 00222 // Seed the generator with a simple uint32 00223 initialize(oneSeed); 00224 reload(); 00225 }
| std::ostream& operator<< | ( | std::ostream & | os, | |
| const MTRand & | mtrand | |||
| ) | [friend] |
Definition at line 370 of file MersenneTwister.h.
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 }
| std::istream& operator>> | ( | std::istream & | is, | |
| MTRand & | mtrand | |||
| ) | [friend] |
Definition at line 379 of file MersenneTwister.h.
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 }
int MTRand::left [protected] |
Definition at line 87 of file MersenneTwister.h.
Referenced by load(), operator<<(), operator>>(), randInt(), reload(), and save().
uint32* MTRand::pNext [protected] |
Definition at line 86 of file MersenneTwister.h.
Referenced by load(), operator>>(), randInt(), and reload().
uint32 MTRand::state[N] [protected] |
Definition at line 85 of file MersenneTwister.h.
Referenced by initialize(), load(), operator<<(), operator>>(), reload(), save(), and seed().
1.5.6