#include <MersenneTwister.h>
Public Types | |
| typedef unsigned long | uint32 |
| enum | { N = 624 } |
| enum | { SAVE = N + 1 } |
Public Methods | |
| MTRand (const uint32 &oneSeed) | |
| MTRand (uint32 *const bigSeed) | |
| 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() () |
| void | seed (uint32 oneSeed) |
| void | seed (uint32 *const bigSeed) |
| void | seed () |
| void | save (uint32 *saveArray) const |
| void | load (uint32 *const loadArray) |
Protected Types | |
| enum | { M = 397 } |
| enum | { MAGIC = 0x9908b0dfU } |
Protected Methods | |
| 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 Methods | |
| uint32 | hash (time_t t, clock_t c) |
Protected Attributes | |
| uint32 | state [N] |
| uint32 * | pNext |
| int | left |
Friends | |
| std::ostream & | operator<< (std::ostream &os, const MTRand &mtrand) |
| std::istream & | operator>> (std::istream &is, MTRand &mtrand) |
|
|
Definition at line 57 of file MersenneTwister.h. Referenced by hash(), hiBit(), load(), loBit(), loBits(), mixBits(), operator<<(), operator>>(), randInt(), reload(), save(), seed(), and twist(). |
|
|
Definition at line 59 of file MersenneTwister.h.
00059 { N = 624 }; // length of state vector
|
|
|
Definition at line 60 of file MersenneTwister.h.
|
|
|
Definition at line 63 of file MersenneTwister.h.
00063 { M = 397 }; // period parameter
|
|
|
Definition at line 64 of file MersenneTwister.h.
00064 { MAGIC = 0x9908b0dfU }; // magic constant
|
|
|
Definition at line 115 of file MersenneTwister.h. References seed().
00116 { seed(oneSeed); }
|
|
|
Definition at line 118 of file MersenneTwister.h. References seed().
00119 { seed(bigSeed); }
|
|
|
Definition at line 121 of file MersenneTwister.h. References seed().
00122 { seed(); }
|
|
||||||||||||
|
Definition at line 251 of file MersenneTwister.h. References uint32. Referenced by seed().
00252 {
00253 // Get a uint32 from t and c
00254 // Better than uint32(x) in case x is floating point in [0,1]
00255 // Based on code by Lawrence Kirby (fred@genesis.demon.co.uk)
00256
00257 static uint32 differ = 0; // guarantee time-based seeds will change
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 }
|
|
|
Definition at line 104 of file MersenneTwister.h. References uint32. Referenced by mixBits().
00104 { return u & 0x80000000U; }
|
|
|
Definition at line 287 of file MersenneTwister.h. References left, N, pNext, state, and uint32.
|
|
|
Definition at line 105 of file MersenneTwister.h. References uint32. Referenced by twist().
00105 { return u & 0x00000001U; }
|
|
|
Definition at line 106 of file MersenneTwister.h. References uint32. Referenced by mixBits().
00106 { return u & 0x7fffffffU; }
|
|
||||||||||||
|
Definition at line 107 of file MersenneTwister.h. References hiBit(), loBits(), and uint32. Referenced by twist().
|
|
|
Definition at line 89 of file MersenneTwister.h. References rand().
00089 { return rand(); } // same as rand()
|
|
|
Definition at line 127 of file MersenneTwister.h. References rand().
00128 { return rand() * n; }
|
|
|
Definition at line 124 of file MersenneTwister.h. References randInt(). Referenced by operator()(), rand(), and matvec::ranf().
00125 { return double(randInt()) * 2.3283064370807974e-10; }
|
|
|
Definition at line 139 of file MersenneTwister.h. References randDblExc().
00140 { return randDblExc() * n; }
|
|
|
Definition at line 136 of file MersenneTwister.h. References randInt(). Referenced by randDblExc().
00137 { return double( 1.0 + randInt() ) * 2.3283064359965952e-10; }
|
|
|
Definition at line 133 of file MersenneTwister.h. References randExc().
00134 { return randExc() * n; }
|
|
|
Definition at line 130 of file MersenneTwister.h. References randInt(). Referenced by randExc().
00131 { return double(randInt()) * 2.3283064365386963e-10; }
|
|
|
Definition at line 156 of file MersenneTwister.h. References randInt(), and uint32.
00157 {
00158 // Find which bits are used in n
00159 uint32 used = ~0;
00160 for( uint32 m = n; m; used <<= 1, m >>= 1 ) {}
00161 used = ~used;
00162
00163 // Draw numbers until one is found in [0,n]
00164 uint32 i;
00165 do
00166 i = randInt() & used; // toss unused bits to shorten search
00167 while( i > n );
00168 return i;
00169 }
|
|
|
Definition at line 142 of file MersenneTwister.h. References left, pNext, reload(), and uint32. Referenced by rand(), randDblExc(), randExc(), and randInt().
|
|
|
Definition at line 235 of file MersenneTwister.h. References left, M, N, pNext, state, twist(), and uint32. Referenced by randInt(), and seed().
00236 {
00237 // Generate N new values in state
00238 // Made clearer and faster by Matthew Bellew (matthew.bellew@home.com)
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 }
|
|
|
Definition at line 277 of file MersenneTwister.h. References left, N, state, and uint32.
|
|
|
Definition at line 202 of file MersenneTwister.h. References hash(), N, reload(), state, and uint32. Referenced by MTRand().
00203 {
00204 // Seed the generator with an array from /dev/urandom if available
00205 // Otherwise use a hash of time() and clock() values
00206
00207 // First try getting an array from /dev/urandom
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; // filter in case uint32 > 32 bits
00218 }
00219 fclose(urandom);
00220 if( success )
00221 {
00222 // There is a 1 in 2^19937 chance that a working urandom gave
00223 // 19937 consecutive zeroes and will make the generator fail
00224 // Ignore that case and continue with initialization
00225 reload();
00226 return;
00227 }
00228 }
00229
00230 // Was not successful, so use time() and clock() instead
00231 seed( hash( time(NULL), clock() ) );
00232 }
|
|
|
Definition at line 186 of file MersenneTwister.h. References N, reload(), state, and uint32.
00187 {
00188 // Seed the generator with an array of 624 uint32's
00189 // There are 2^19937-1 possible initial states. This function allows
00190 // any one of those to be chosen by providing 19937 bits. The lower
00191 // 31 bits of the first element, bigSeed[0], are discarded. Any bits
00192 // above the lower 32 in each element are also discarded. Theoretically,
00193 // the rest of the array can contain any values except all zeroes.
00194 // Just call seed() if you want to get array from /dev/urandom
00195 register uint32 *s = state, *b = bigSeed;
00196 register int i = N;
00197 for( ; i--; *s++ = *b++ & 0xffffffff ) {}
00198 reload();
00199 }
|
|
|
Definition at line 172 of file MersenneTwister.h. References N, reload(), state, and uint32.
00173 {
00174 // Seed the generator with a simple uint32
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)++ ) {} // hard to read, but fast
00182 reload();
00183 }
|
|
||||||||||||||||
|
Definition at line 109 of file MersenneTwister.h. References loBit(), MAGIC, mixBits(), and uint32. Referenced by reload().
|
|
||||||||||||
|
Definition at line 298 of file MersenneTwister.h.
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 }
|
|
||||||||||||
|
Definition at line 307 of file MersenneTwister.h.
|
|
|
Definition at line 68 of file MersenneTwister.h. Referenced by load(), operator<<(), operator>>(), randInt(), reload(), and save(). |
|
|
Definition at line 67 of file MersenneTwister.h. Referenced by load(), operator>>(), randInt(), and reload(). |
|
|
Definition at line 66 of file MersenneTwister.h. Referenced by load(), operator<<(), operator>>(), reload(), save(), and seed(). |
1.2.16