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)
102 double rand(
const double& n );
104 double randExc(
const double& n );
115 double randNorm(
const double& mean = 0.0,
const double& variance = 0.0 );
138 {
return m ^ (
mixBits(s0,s1)>>1) ^ (s1 & 1UL ? 0x9908b0dfUL : 0); }
147 {
seed(bigSeed,seedLength); }
153 {
return double(
randInt()) * (1.0/4294967295.0); }
156 {
return rand() * n; }
159 {
return double(
randInt()) * (1.0/4294967296.0); }
165 {
return (
double(
randInt()) + 0.5 ) * (1.0/4294967296.0); }
173 return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0);
180 double r = sqrt( -2.0 * log( 1.0-
randDblExc()) ) * variance;
181 double phi = 2.0 * 3.14159265358979323846264338328 *
randExc();
182 return mean + r * cos(phi);
196 s1 ^= (s1 << 7) & 0x9d2c5680UL;
197 s1 ^= (s1 << 15) & 0xefc60000UL;
198 return ( s1 ^ (s1 >> 18) );
241 if( seedLength > k ) k = seedLength;
246 state[i] += ( bigSeed[j] & 0xffffffffUL ) + j;
247 state[i] &= 0xffffffffUL;
249 if( i >=
N ) { state[0] = state[
N-1]; i = 1; }
250 if( j >= seedLength ) j = 0;
252 for( k =
N - 1; k; --k )
257 state[i] &= 0xffffffffUL;
259 if( i >=
N ) { state[0] = state[
N-1]; i = 1; }
261 state[0] = 0x80000000UL;
272 FILE* urandom = fopen(
"/dev/urandom",
"rb" );
276 register uint32 *s = bigSeed;
278 register bool success =
true;
279 while( success && i-- )
280 success = fread( s++,
sizeof(
uint32), 1, urandom )!=0;
282 if( success ) {
seed( bigSeed,
N );
return; }
286 seed(
hash( time(NULL), clock() ) );
299 *s++ = seed & 0xffffffffUL;
302 *s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL;
314 for( i =
N -
M; i--; ++p )
315 *p =
twist( p[
M], p[0], p[1] );
316 for( i = M; --i; ++p )
317 *p =
twist( p[M-
N], p[0], p[1] );
333 unsigned char *p = (
unsigned char *) &t;
334 for(
size_t i = 0; i <
sizeof(t); ++i )
336 h1 *= UCHAR_MAX + 2U;
340 p = (
unsigned char *) &c;
341 for(
size_t j = 0; j <
sizeof(c); ++j )
343 h2 *= UCHAR_MAX + 2U;
346 return ( h1 + differ++ ) ^ h2;
352 register uint32 *sa = saveArray;
355 for( ; i--; *sa++ = *s++ ) {}
363 register uint32 *la = loadArray;
365 for( ; i--; *s++ = *la++ ) {}
374 register int i = mtrand.
N;
375 for( ; i--; os << *s++ <<
"\t" ) {}
376 return os << mtrand.
left;
383 register int i = mtrand.
N;
384 for( ; i--; is >> *s++ ) {}
390 #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
std::istream & operator>>(std::istream &is, MTRand &mtrand)
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)
std::ostream & operator<<(std::ostream &os, const MTRand &mtrand)