61#ifndef MERSENNETWISTER_H
62#define MERSENNETWISTER_H
101 double rand(
const double& n );
103 double randExc(
const double& n );
114 double randNorm(
const double& mean = 0.0,
const double& variance = 1.0 );
136 {
return m ^ (
mixBits(s0,s1)>>1) ^ (-
loBit(s1) & 0x9908b0dfUL); }
145{
seed(bigSeed,seedLength); }
151{
return double(
randInt()) * (1.0/4294967295.0); }
154{
return rand() * n; }
157{
return double(
randInt()) * (1.0/4294967296.0); }
163{
return (
double(
randInt()) + 0.5 ) * (1.0/4294967296.0); }
171 return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0);
179 double phi = 2.0 * 3.14159265358979323846264338328 *
randExc();
180 return mean + r * cos(phi);
194 s1 ^= (s1 << 7) & 0x9d2c5680UL;
195 s1 ^= (s1 << 15) & 0xefc60000UL;
196 return ( s1 ^ (s1 >> 18) );
238 register int k = (
uint32(
N) > seedLength ?
uint32(
N) : seedLength );
243 state[i] += ( bigSeed[j] & 0xffffffffUL ) + j;
244 state[i] &= 0xffffffffUL;
247 if( j >= seedLength ) j = 0;
249 for( k =
N - 1; k; --k )
254 state[i] &= 0xffffffffUL;
258 state[0] = 0x80000000UL;
269 FILE* urandom = fopen(
"/dev/urandom",
"rb" );
273 register uint32 *s = bigSeed;
275 register bool success =
true;
276 while( success && i-- )
277 success = fread( s++,
sizeof(
uint32), 1, urandom );
279 if( success ) {
seed( bigSeed,
N );
return; }
283 seed(
hash( time(NULL), clock() ) );
296 *s++ =
seed & 0xffffffffUL;
299 *s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL;
311 for( i =
N -
M; i--; ++p )
312 *p =
twist( p[
M], p[0], p[1] );
313 for( i =
M; --i; ++p )
314 *p =
twist( p[
M-
N], p[0], p[1] );
330 unsigned char *p = (
unsigned char *) &t;
331 for(
size_t i = 0; i <
sizeof(t); ++i )
333 h1 *= UCHAR_MAX + 2U;
337 p = (
unsigned char *) &
c;
338 for(
size_t j = 0; j <
sizeof(
c); ++j )
340 h2 *= UCHAR_MAX + 2U;
343 return ( h1 + differ++ ) ^ h2;
349 register uint32 *sa = saveArray;
352 for( ; i--; *sa++ = *s++ ) {}
360 register uint32 *la = loadArray;
362 for( ; i--; *s++ = *la++ ) {}
368inline std::ostream& operator<<( std::ostream& os,
const MTRand& mtrand )
371 register int i = mtrand.
N;
372 for( ; i--; os << *s++ <<
"\t" ) {}
373 return os << mtrand.
left;
380 register int i = mtrand.
N;
381 for( ; i--;
is >> *s++ ) {}
constexpr auto is
Equivalent to the boolean value of dynamic_cast<T const *>(...).
double randNorm(const double &mean=0.0, const double &variance=1.0)
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
std::istream & operator>>(std::istream &is, MTRand &mtrand)
Piecewise< SBasis > log(Interval in)