#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) | |
MTRand () | |
double | rand () |
double | rand (const double &n) |
double | randExc () |
double | randExc (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 Member Functions | |
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 |
Friends | |
ostream & | operator<< (ostream &os, const MTRand &mtrand) |
istream & | operator>> (istream &is, MTRand &mtrand) |
Definition at line 53 of file MersenneTwister.h.
typedef unsigned long MTRand::uint32 |
Definition at line 56 of file MersenneTwister.h.
anonymous enum |
anonymous enum |
anonymous enum [protected] |
anonymous enum [protected] |
MTRand::MTRand | ( | const uint32 & | oneSeed | ) | [inline] |
MTRand::MTRand | ( | uint32 *const | bigSeed | ) | [inline] |
MTRand::MTRand | ( | ) | [inline] |
MTRand::uint32 MTRand::hash | ( | time_t | t, | |
clock_t | c | |||
) | [inline, static, protected] |
Definition at line 224 of file MersenneTwister.h.
00225 { 00226 // Get a uint32 from t and c 00227 // Better than uint32(x) in case x is floating point in [0,1] 00228 // Based on code by Lawrence Kirby (fred@genesis.demon.co.uk) 00229 00230 static uint32 differ = 0; // guarantee time-based seeds will change 00231 00232 uint32 h1 = 0; 00233 unsigned char *p = (unsigned char *) &t; 00234 for( size_t i = 0; i < sizeof(t); ++i ) 00235 { 00236 h1 *= UCHAR_MAX + 2U; 00237 h1 += p[i]; 00238 } 00239 uint32 h2 = 0; 00240 p = (unsigned char *) &c; 00241 for( size_t j = 0; j < sizeof(c); ++j ) 00242 { 00243 h2 *= UCHAR_MAX + 2U; 00244 h2 += p[j]; 00245 } 00246 return ( h1 + differ++ ) ^ h2; 00247 }
void MTRand::load | ( | uint32 *const | loadArray | ) | [inline] |
double MTRand::operator() | ( | ) | [inline] |
double MTRand::rand | ( | const double & | n | ) | [inline] |
double MTRand::rand | ( | ) | [inline] |
Definition at line 121 of file MersenneTwister.h.
00122 { return double(randInt()) * 2.3283064370807974e-10; }
double MTRand::randExc | ( | const double & | n | ) | [inline] |
double MTRand::randExc | ( | ) | [inline] |
Definition at line 127 of file MersenneTwister.h.
00128 { return double(randInt()) * 2.3283064365386963e-10; }
MTRand::uint32 MTRand::randInt | ( | const uint32 & | n | ) | [inline] |
MTRand::uint32 MTRand::randInt | ( | ) | [inline] |
void MTRand::reload | ( | ) | [inline, protected] |
Definition at line 208 of file MersenneTwister.h.
00209 { 00210 // Generate N new values in state 00211 // Made clearer and faster by Matthew Bellew (matthew.bellew@home.com) 00212 register uint32 *p = state; 00213 register int i; 00214 for( i = N - M; i--; ) 00215 *p++ = twist( p[M], p[0], p[1] ); 00216 for( i = M; --i; ) 00217 *p++ = twist( p[M-N], p[0], p[1] ); 00218 *p = twist( p[M-N], p[0], state[0] ); 00219 00220 left = N, pNext = state; 00221 }
void MTRand::save | ( | uint32 * | saveArray | ) | const [inline] |
void MTRand::seed | ( | ) | [inline] |
Definition at line 180 of file MersenneTwister.h.
00181 { 00182 /* commented G.K. 00183 // Seed the generator with an array from /dev/urandom if available 00184 // Otherwise use a hash of time() and clock() values 00185 00186 // First try getting an array from /dev/urandom 00187 FILE* urandom = fopen( "/dev/urandom", "rb" ); 00188 if( urandom ) 00189 { 00190 register uint32 *s = state; 00191 register int i = N; 00192 register bool success = true; 00193 for( ; success && i--; 00194 success = fread( s++, sizeof(uint32), 1, urandom ) ) {} 00195 fclose(urandom); 00196 if( success ) 00197 { 00198 reload(); 00199 return; 00200 } 00201 } 00202 */ 00203 // Was not successful, so use time() and clock() instead 00204 seed( hash( time((long*)NULL), clock() ) ); 00205 }
void MTRand::seed | ( | uint32 *const | bigSeed | ) | [inline] |
Definition at line 166 of file MersenneTwister.h.
00167 { 00168 // Seed the generator with an array of 624 uint32's 00169 // There are 2^19937-1 possible initial states. This function 00170 // allows any one of those to be chosen by providing 19937 bits. 00171 // Theoretically, the array can contain any values except all zeroes. 00172 // Just call seed() if you want to get array from /dev/urandom 00173 register uint32 *s = state, *b = bigSeed; 00174 register int i = N; 00175 for( ; i--; *s++ = *b++ ) {} 00176 reload(); 00177 }
void MTRand::seed | ( | uint32 | oneSeed | ) | [inline] |
Definition at line 152 of file MersenneTwister.h.
00153 { 00154 // Seed the generator with a simple uint32 00155 register uint32 *s; 00156 register int i; 00157 for( i = N, s = state; 00158 i--; 00159 *s = oneSeed & 0xffff0000, 00160 *s++ |= ( (oneSeed *= 69069U)++ & 0xffff0000 ) >> 16, 00161 (oneSeed *= 69069U)++ ) {} // hard to read, but fast 00162 reload(); 00163 }
uint32 MTRand::twist | ( | const uint32 & | m, | |
const uint32 & | s0, | |||
const uint32 & | s1 | |||
) | const [inline, protected] |
ostream& operator<< | ( | ostream & | os, | |
const MTRand & | mtrand | |||
) | [friend] |
Definition at line 271 of file MersenneTwister.h.
00272 { 00273 register const MTRand::uint32 *s = mtrand.state; 00274 register int i = mtrand.N; 00275 for( ; i--; os << *s++ << "\t" ) {} 00276 return os << mtrand.left; 00277 }
istream& operator>> | ( | istream & | is, | |
MTRand & | mtrand | |||
) | [friend] |
Definition at line 280 of file MersenneTwister.h.
00281 { 00282 register MTRand::uint32 *s = mtrand.state; 00283 register int i = mtrand.N; 00284 for( ; i--; is >> *s++ ) {} 00285 is >> mtrand.left; 00286 mtrand.pNext = &mtrand.state[mtrand.N-mtrand.left]; 00287 return is; 00288 }
int MTRand::left [protected] |
Definition at line 67 of file MersenneTwister.h.
uint32* MTRand::pNext [protected] |
Definition at line 66 of file MersenneTwister.h.
uint32 MTRand::state[N] [protected] |
Definition at line 65 of file MersenneTwister.h.