59#ifndef MERSENNETWISTER_H
60#define MERSENNETWISTER_H
99 double rand(
const double& n );
101 double randExc(
const double& n );
112 double randNorm(
const double& mean = 0.0,
const double& variance = 0.0 );
134 {
return m ^ (
mixBits(s0,s1)>>1) ^ (-
loBit(s1) & 0x9908b0dfUL); }
143 {
seed(bigSeed,seedLength); }
149 {
return double(
randInt()) * (1.0/4294967295.0); }
152 {
return rand() * n; }
155 {
return double(
randInt()) * (1.0/4294967296.0); }
161 {
return (
double(
randInt()) + 0.5 ) * (1.0/4294967296.0); }
169 return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0);
176 double r = sqrt( -2.0 * log( 1.0-
randDblExc()) ) * variance;
177 double phi = 2.0 * 3.14159265358979323846264338328 *
randExc();
178 return mean + r * cos(phi);
192 s1 ^= (s1 << 7) & 0x9d2c5680UL;
193 s1 ^= (s1 << 15) & 0xefc60000UL;
194 return ( s1 ^ (s1 >> 18) );
236 int k = (
N > seedLength ?
N : seedLength );
241 state[i] += ( bigSeed[j] & 0xffffffffUL ) + j;
242 state[i] &= 0xffffffffUL;
245 if( j >= seedLength ) j = 0;
247 for( k =
N - 1; k; --k )
252 state[i] &= 0xffffffffUL;
256 state[0] = 0x80000000UL;
267 FILE* urandom = fopen(
"/dev/urandom",
"rb" );
274 while( success && i-- )
275 success = fread( s++,
sizeof(
uint32), 1, urandom );
277 if( success ) {
seed( bigSeed,
N );
return; }
281 seed(
hash( time(NULL), clock() ) );
294 *s++ =
seed & 0xffffffffUL;
297 *s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL;
309 for( i =
N -
M; i--; ++p )
310 *p =
twist( p[
M], p[0], p[1] );
311 for( i =
M; --i; ++p )
312 *p =
twist( p[
M-
N], p[0], p[1] );
328 unsigned char *p = (
unsigned char *) &t;
329 for(
size_t i = 0; i <
sizeof(t); ++i )
331 h1 *= UCHAR_MAX + 2U;
335 p = (
unsigned char *) &c;
336 for(
size_t j = 0; j <
sizeof(c); ++j )
338 h2 *= UCHAR_MAX + 2U;
341 return ( h1 + differ++ ) ^ h2;
350 for( ; i--; *sa++ = *s++ ) {}
360 for( ; i--; *s++ = *la++ ) {}
370 for( ; i--; os << *s++ <<
"\t" ) {}
371 return os << mtrand.
left;
379 for( ; i--; is >> *s++ ) {}
std::ostream & operator<<(std::ostream &os, const MTRand &mtrand)
std::istream & operator>>(std::istream &is, MTRand &mtrand)
uint32 loBits(const uint32 &u) const
uint32 mixBits(const uint32 &u, const uint32 &v) const
static uint32 hash(time_t t, clock_t c)
friend std::ostream & operator<<(std::ostream &os, const MTRand &mtrand)
uint32 loBit(const uint32 &u) const
void load(uint32 *const loadArray)
uint32 hiBit(const uint32 &u) const
void initialize(const uint32 oneSeed)
void save(uint32 *saveArray) const
friend std::istream & operator>>(std::istream &is, MTRand &mtrand)
uint32 twist(const uint32 &m, const uint32 &s0, const uint32 &s1) const
double randNorm(const double &mean=0.0, const double &variance=0.0)