57 #ifndef MERSENNETWISTER_H 58 #define MERSENNETWISTER_H 87 MTRand(
const uint32& oneSeed );
88 MTRand( uint32 *
const bigSeed, uint32
const seedLength =
N );
97 double rand(
const double& n );
99 double randExc(
const double& n );
103 uint32
randInt(
const uint32& n );
110 double randNorm(
const double& mean = 0.0,
const double& variance = 0.0 );
113 void seed(
const uint32 oneSeed );
114 void seed( uint32 *
const bigSeed,
const uint32 seedLength =
N );
118 void save( uint32* saveArray )
const;
119 void load( uint32 *
const loadArray );
126 uint32
hiBit(
const uint32& u )
const {
return u & 0x80000000UL; }
127 uint32
loBit(
const uint32& u )
const {
return u & 0x00000001UL; }
128 uint32
loBits(
const uint32& u )
const {
return u & 0x7fffffffUL; }
129 uint32
mixBits(
const uint32& u,
const uint32& v )
const 131 uint32
twist(
const uint32& m,
const uint32& s0,
const uint32& s1 )
const 132 {
return m ^ (
mixBits(s0,s1)>>1) ^ (-
loBit(s1) & 0x9908b0dfUL); }
133 static uint32
hash( time_t t, clock_t c );
141 {
seed(bigSeed,seedLength); }
147 {
return double(
randInt()) * (1.0/4294967295.0); }
150 {
return rand() * n; }
153 {
return double(
randInt()) * (1.0/4294967296.0); }
159 {
return (
double(
randInt()) + 0.5 ) * (1.0/4294967296.0); }
167 return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0);
174 double r = sqrt( -2.0 * log( 1.0-
randDblExc()) ) * variance;
175 double phi = 2.0 * 3.14159265358979323846264338328 *
randExc();
176 return mean + r * cos(phi);
190 s1 ^= (s1 << 7) & 0x9d2c5680UL;
191 s1 ^= (s1 << 15) & 0xefc60000UL;
192 return ( s1 ^ (s1 >> 18) );
234 int k = (
N > seedLength ?
N : seedLength );
239 state[i] += ( bigSeed[j] & 0xffffffffUL ) + j;
240 state[i] &= 0xffffffffUL;
242 if( i >=
N ) { state[0] = state[
N-1]; i = 1; }
243 if( j >= seedLength ) j = 0;
245 for( k =
N - 1; k; --k )
250 state[i] &= 0xffffffffUL;
252 if( i >=
N ) { state[0] = state[
N-1]; i = 1; }
254 state[0] = 0x80000000UL;
265 FILE* urandom = fopen(
"/dev/urandom",
"rb" );
272 while( success && i-- )
273 success = fread( s++,
sizeof(
uint32), 1, urandom );
275 if( success ) {
seed( bigSeed,
N );
return; }
279 seed(
hash( time(NULL), clock() ) );
292 *s++ = seed & 0xffffffffUL;
295 *s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL;
307 for( i =
N -
M; i--; ++p )
308 *p =
twist( p[
M], p[0], p[1] );
309 for( i = M; --i; ++p )
310 *p =
twist( p[M-
N], p[0], p[1] );
326 unsigned char *p = (
unsigned char *) &t;
327 for(
size_t i = 0; i <
sizeof(t); ++i )
329 h1 *= UCHAR_MAX + 2U;
333 p = (
unsigned char *) &c;
334 for(
size_t j = 0; j <
sizeof(c); ++j )
336 h2 *= UCHAR_MAX + 2U;
339 return ( h1 + differ++ ) ^ h2;
348 for( ; i--; *sa++ = *s++ ) {}
358 for( ; i--; *s++ = *la++ ) {}
368 for( ; i--; os << *s++ <<
"\t" ) {}
369 return os << mtrand.
left;
377 for( ; i--; is >> *s++ ) {}
383 #endif // MERSENNETWISTER_H
void load(uint32 *const loadArray)
uint32 hiBit(const uint32 &u) const
uint32 mixBits(const uint32 &u, const uint32 &v) const
friend std::istream & operator>>(std::istream &is, MTRand &mtrand)
uint32 loBit(const uint32 &u) const
static uint32 hash(time_t t, clock_t c)
void initialize(const uint32 oneSeed)
uint32 twist(const uint32 &m, const uint32 &s0, const uint32 &s1) const
void save(uint32 *saveArray) const
double randNorm(const double &mean=0.0, const double &variance=0.0)
friend std::ostream & operator<<(std::ostream &os, const MTRand &mtrand)
uint32 loBits(const uint32 &u) const