57 #ifndef MERSENNETWISTER_H 58 #define MERSENNETWISTER_H 63 #ifdef _MSC_VER // !!! mb pragmas added 19.02.2007 64 #pragma warning(disable: 4146) 65 #pragma warning(disable: 4996) 79 typedef unsigned long long int uint64;
94 MTRand(
const uint32& oneSeed );
95 MTRand( uint32 *
const bigSeed, uint32
const seedLength =
N );
104 double rand(
const double& n );
106 double randExc(
const double& n );
110 uint32
randInt(
const uint32& n );
119 double randNorm(
const double& mean = 0.0,
const double& variance = 0.0 );
122 void seed(
const uint32 oneSeed );
123 void seed( uint32 *
const bigSeed,
const uint32 seedLength =
N );
127 void save( uint32* saveArray )
const;
128 void load( uint32 *
const loadArray );
132 static uint32
hash( time_t t, clock_t c );
137 uint32
hiBit(
const uint32& u )
const {
return u & 0x80000000UL; }
138 uint32
loBits(
const uint32& u )
const {
return u & 0x7fffffffUL; }
139 uint32
mixBits(
const uint32& u,
const uint32& v )
const 141 uint32
twist(
const uint32& m,
const uint32& s0,
const uint32& s1 )
const 142 {
return m ^ (
mixBits(s0,s1)>>1) ^ (s1 & 1UL ? 0x9908b0dfUL : 0); }
151 {
seed(bigSeed,seedLength); }
157 {
return double(
randInt()) * (1.0/4294967295.0); }
160 {
return rand() * n; }
163 {
return double(
randInt()) * (1.0/4294967296.0); }
169 {
return (
double(
randInt()) + 0.5 ) * (1.0/4294967296.0); }
177 return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0);
184 double r = sqrt( -2.0 * log( 1.0-
randDblExc()) ) * variance;
185 double phi = 2.0 * 3.14159265358979323846264338328 *
randExc();
186 return mean + r * cos(phi);
200 s1 ^= (s1 << 7) & 0x9d2c5680UL;
201 s1 ^= (s1 << 15) & 0xefc60000UL;
202 return ( s1 ^ (s1 >> 18) );
269 if( seedLength > k ) k = seedLength;
274 state[i] += ( bigSeed[j] & 0xffffffffUL ) + j;
275 state[i] &= 0xffffffffUL;
277 if( i >=
N ) { state[0] = state[
N-1]; i = 1; }
278 if( j >= seedLength ) j = 0;
280 for( k =
N - 1; k; --k )
285 state[i] &= 0xffffffffUL;
287 if( i >=
N ) { state[0] = state[
N-1]; i = 1; }
289 state[0] = 0x80000000UL;
300 FILE* urandom = fopen(
"/dev/urandom",
"rb" );
304 register uint32 *s = bigSeed;
306 register bool success =
true;
307 while( success && i-- )
308 success = fread( s++,
sizeof(
uint32), 1, urandom )!=0;
310 if( success ) {
seed( bigSeed,
N );
return; }
314 seed(
hash( time(NULL), clock() ) );
327 *s++ = seed & 0xffffffffUL;
330 *s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL;
342 for( i =
N -
M; i--; ++p )
343 *p =
twist( p[
M], p[0], p[1] );
344 for( i = M; --i; ++p )
345 *p =
twist( p[M-
N], p[0], p[1] );
361 unsigned char *p = (
unsigned char *) &t;
362 for(
size_t i = 0; i <
sizeof(t); ++i )
364 h1 *= UCHAR_MAX + 2U;
368 p = (
unsigned char *) &c;
369 for(
size_t j = 0; j <
sizeof(c); ++j )
371 h2 *= UCHAR_MAX + 2U;
374 return ( h1 + differ++ ) ^ h2;
380 register uint32 *sa = saveArray;
383 for( ; i--; *sa++ = *s++ ) {}
391 register uint32 *la = loadArray;
393 for( ; i--; *s++ = *la++ ) {}
402 register int i = mtrand.
N;
403 for( ; i--; os << *s++ <<
"\t" ) {}
404 return os << mtrand.
left;
411 register int i = mtrand.
N;
412 for( ; i--; is >> *s++ ) {}
418 #endif // MERSENNETWISTER_H
void load(uint32 *const loadArray)
friend std::istream & operator>>(std::istream &is, MTRand &mtrand)
uint32 twist(const uint32 &m, const uint32 &s0, const uint32 &s1) const
uint32 mixBits(const uint32 &u, const uint32 &v) const
static uint32 hash(time_t t, clock_t c)
void initialize(const uint32 oneSeed)
uint32 loBits(const uint32 &u) const
uint64 randInt64(const uint64 &n)
double randNorm(const double &mean=0.0, const double &variance=0.0)
void save(uint32 *saveArray) const
uint32 hiBit(const uint32 &u) const
friend std::ostream & operator<<(std::ostream &os, const MTRand &mtrand)
unsigned long long int uint64