#include <MersenneTwister.h>
Public Types | |
enum | { N = 624 } |
enum | { SAVE = N + 1 } |
typedef unsigned long | uint32 |
Public Member Functions | |
MTRand (const uint32 &oneSeed) | |
MTRand (uint32 *const bigSeed, uint32 const seedLength=N) | |
MTRand () | |
double | rand () |
double | rand (const double &n) |
double | randExc () |
double | randExc (const double &n) |
double | randDblExc () |
double | randDblExc (const double &n) |
uint32 | randInt () |
uint32 | randInt (const uint32 &n) |
double | operator() () |
double | rand53 () |
double | randNorm (const double &mean=0.0, const double &variance=0.0) |
void | seed (const uint32 oneSeed) |
void | seed (uint32 *const bigSeed, const uint32 seedLength=N) |
void | seed () |
void | save (uint32 *saveArray) const |
void | load (uint32 *const loadArray) |
Protected Types | |
enum | { M = 397 } |
Protected Member Functions | |
void | initialize (const uint32 oneSeed) |
void | reload () |
uint32 | hiBit (const uint32 &u) const |
uint32 | loBit (const uint32 &u) const |
uint32 | loBits (const uint32 &u) const |
uint32 | mixBits (const uint32 &u, const uint32 &v) const |
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 | |
uint32 | state [N] |
uint32 * | pNext |
int | left |
Definition at line 71 of file MersenneTwister.h.
typedef unsigned long MTRand::uint32 |
Definition at line 74 of file MersenneTwister.h.
anonymous enum |
anonymous enum |
anonymous enum [protected] |
MTRand::MTRand | ( | const uint32 & | oneSeed | ) | [inline] |
Definition at line 139 of file MersenneTwister.h.
References seed().
00140 { seed(oneSeed); }
Definition at line 142 of file MersenneTwister.h.
References seed().
00143 { seed(bigSeed,seedLength); }
MTRand::MTRand | ( | ) | [inline] |
Definition at line 145 of file MersenneTwister.h.
References seed().
00146 { seed(); }
double MTRand::rand | ( | ) | [inline] |
Definition at line 148 of file MersenneTwister.h.
References randInt().
Referenced by CDistribution::BoxMuller(), CDistribution::GenerateExponential(), CDistribution::GenerateGamma(), CDistribution::GenerateGEV(), CDistribution::GenerateGumbel(), CDistribution::GenerateLogNormal(), CDistribution::GeneratePoisson(), operator()(), and rand().
00149 { return double(randInt()) * (1.0/4294967295.0); }
double MTRand::rand | ( | const double & | n | ) | [inline] |
Definition at line 151 of file MersenneTwister.h.
References rand().
00152 { return rand() * n; }
double MTRand::randExc | ( | ) | [inline] |
Definition at line 154 of file MersenneTwister.h.
References randInt().
Referenced by randExc(), and randNorm().
00155 { return double(randInt()) * (1.0/4294967296.0); }
double MTRand::randExc | ( | const double & | n | ) | [inline] |
Definition at line 157 of file MersenneTwister.h.
References randExc().
00158 { return randExc() * n; }
double MTRand::randDblExc | ( | ) | [inline] |
Definition at line 160 of file MersenneTwister.h.
References randInt().
Referenced by randDblExc(), and randNorm().
00161 { return ( double(randInt()) + 0.5 ) * (1.0/4294967296.0); }
double MTRand::randDblExc | ( | const double & | n | ) | [inline] |
Definition at line 163 of file MersenneTwister.h.
References randDblExc().
00164 { return randDblExc() * n; }
MTRand::uint32 MTRand::randInt | ( | ) | [inline] |
Definition at line 181 of file MersenneTwister.h.
References left, pNext, and reload().
Referenced by rand(), rand53(), randDblExc(), randExc(), and randInt().
00182 { 00183 // Pull a 32-bit integer from the generator state 00184 // Every other access function simply transforms the numbers extracted here 00185 00186 if( left == 0 ) reload(); 00187 --left; 00188 00189 register uint32 s1; 00190 s1 = *pNext++; 00191 s1 ^= (s1 >> 11); 00192 s1 ^= (s1 << 7) & 0x9d2c5680UL; 00193 s1 ^= (s1 << 15) & 0xefc60000UL; 00194 return ( s1 ^ (s1 >> 18) ); 00195 }
MTRand::uint32 MTRand::randInt | ( | const uint32 & | n | ) | [inline] |
Definition at line 197 of file MersenneTwister.h.
References randInt().
00198 { 00199 // Find which bits are used in n 00200 // Optimized by Magnus Jonsson (magnus@smartelectronix.com) 00201 uint32 used = n; 00202 used |= used >> 1; 00203 used |= used >> 2; 00204 used |= used >> 4; 00205 used |= used >> 8; 00206 used |= used >> 16; 00207 00208 // Draw numbers until one is found in [0,n] 00209 uint32 i; 00210 do 00211 i = randInt() & used; // toss unused bits to shorten search 00212 while( i > n ); 00213 return i; 00214 }
double MTRand::operator() | ( | ) | [inline] |
Definition at line 106 of file MersenneTwister.h.
References rand().
00106 { return rand(); } // same as rand()
double MTRand::rand53 | ( | ) | [inline] |
double MTRand::randNorm | ( | const double & | mean = 0.0 , |
|
const double & | variance = 0.0 | |||
) | [inline] |
Definition at line 172 of file MersenneTwister.h.
References randDblExc(), and randExc().
00173 { 00174 // Return a real number from a normal (Gaussian) distribution with given 00175 // mean and variance by Box-Muller method 00176 double r = sqrt( -2.0 * log( 1.0-randDblExc()) ) * variance; 00177 double phi = 2.0 * 3.14159265358979323846264338328 * randExc(); 00178 return mean + r * cos(phi); 00179 }
void MTRand::seed | ( | const uint32 | oneSeed | ) | [inline] |
Definition at line 217 of file MersenneTwister.h.
References initialize(), and reload().
00218 { 00219 // Seed the generator with a simple uint32 00220 initialize(oneSeed); 00221 reload(); 00222 }
Definition at line 225 of file MersenneTwister.h.
References initialize(), N, reload(), and state.
00226 { 00227 // Seed the generator with an array of uint32's 00228 // There are 2^19937-1 possible initial states. This function allows 00229 // all of those to be accessed by providing at least 19937 bits (with a 00230 // default seed length of N = 624 uint32's). Any bits above the lower 32 00231 // in each element are discarded. 00232 // Just call seed() if you want to get array from /dev/urandom 00233 initialize(19650218UL); 00234 register int i = 1; 00235 register uint32 j = 0; 00236 register int k = ( N > seedLength ? N : seedLength ); 00237 for( ; k; --k ) 00238 { 00239 state[i] = 00240 state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1664525UL ); 00241 state[i] += ( bigSeed[j] & 0xffffffffUL ) + j; 00242 state[i] &= 0xffffffffUL; 00243 ++i; ++j; 00244 if( i >= N ) { state[0] = state[N-1]; i = 1; } 00245 if( j >= seedLength ) j = 0; 00246 } 00247 for( k = N - 1; k; --k ) 00248 { 00249 state[i] = 00250 state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1566083941UL ); 00251 state[i] -= i; 00252 state[i] &= 0xffffffffUL; 00253 ++i; 00254 if( i >= N ) { state[0] = state[N-1]; i = 1; } 00255 } 00256 state[0] = 0x80000000UL; // MSB is 1, assuring non-zero initial array 00257 reload(); 00258 }
void MTRand::seed | ( | ) | [inline] |
Definition at line 261 of file MersenneTwister.h.
Referenced by MTRand().
00262 { 00263 // Seed the generator with an array from /dev/urandom if available 00264 // Otherwise use a hash of time() and clock() values 00265 00266 // First try getting an array from /dev/urandom 00267 FILE* urandom = fopen( "/dev/urandom", "rb" ); 00268 if( urandom ) 00269 { 00270 uint32 bigSeed[N]; 00271 register uint32 *s = bigSeed; 00272 register int i = N; 00273 register bool success = true; 00274 while( success && i-- ) 00275 success = fread( s++, sizeof(uint32), 1, urandom ) == 0 ? false: true; 00276 fclose(urandom); 00277 if( success ) { seed( bigSeed, N ); return; } 00278 } 00279 00280 // Was not successful, so use time() and clock() instead 00281 seed( hash( time(NULL), clock() ) ); 00282 }
void MTRand::save | ( | uint32 * | saveArray | ) | const [inline] |
void MTRand::load | ( | uint32 *const | loadArray | ) | [inline] |
void MTRand::initialize | ( | const uint32 | oneSeed | ) | [inline, protected] |
Definition at line 285 of file MersenneTwister.h.
Referenced by seed().
00286 { 00287 // Initialize generator state with seed 00288 // See Knuth TAOCP Vol 2, 3rd Ed, p.106 for multiplier. 00289 // In previous versions, most significant bits (MSBs) of the seed affect 00290 // only MSBs of the state array. Modified 9 Jan 2002 by Makoto Matsumoto. 00291 register uint32 *s = state; 00292 register uint32 *r = state; 00293 register int i = 1; 00294 *s++ = seed & 0xffffffffUL; 00295 for( ; i < N; ++i ) 00296 { 00297 *s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL; 00298 r++; 00299 } 00300 }
void MTRand::reload | ( | ) | [inline, protected] |
Definition at line 303 of file MersenneTwister.h.
References left, M, N, pNext, state, and twist().
Referenced by randInt(), and seed().
00304 { 00305 // Generate N new values in state 00306 // Made clearer and faster by Matthew Bellew (matthew.bellew@home.com) 00307 register uint32 *p = state; 00308 register int i; 00309 for( i = N - M; i--; ++p ) 00310 *p = twist( p[M], p[0], p[1] ); 00311 for( i = M; --i; ++p ) 00312 *p = twist( p[M-N], p[0], p[1] ); 00313 *p = twist( p[M-N], p[0], state[0] ); 00314 00315 left = N, pNext = state; 00316 }
MTRand::uint32 MTRand::hash | ( | time_t | t, | |
clock_t | c | |||
) | [inline, static, protected] |
Definition at line 319 of file MersenneTwister.h.
Referenced by seed().
00320 { 00321 // Get a uint32 from t and c 00322 // Better than uint32(x) in case x is floating point in [0,1] 00323 // Based on code by Lawrence Kirby (fred@genesis.demon.co.uk) 00324 00325 static uint32 differ = 0; // guarantee time-based seeds will change 00326 00327 uint32 h1 = 0; 00328 unsigned char *p = (unsigned char *) &t; 00329 for( size_t i = 0; i < sizeof(t); ++i ) 00330 { 00331 h1 *= UCHAR_MAX + 2U; 00332 h1 += p[i]; 00333 } 00334 uint32 h2 = 0; 00335 p = (unsigned char *) &c; 00336 for( size_t j = 0; j < sizeof(c); ++j ) 00337 { 00338 h2 *= UCHAR_MAX + 2U; 00339 h2 += p[j]; 00340 } 00341 return ( h1 + differ++ ) ^ h2; 00342 }
uint32 MTRand::state[N] [protected] |
Definition at line 82 of file MersenneTwister.h.
Referenced by initialize(), load(), reload(), save(), and seed().
uint32* MTRand::pNext [protected] |
int MTRand::left [protected] |