57 #ifndef MERSENNETWISTER_H
58 #define MERSENNETWISTER_H
98 double rand(
const double& n );
100 double randExc(
const double& n );
133 {
return m ^ (
mixBits(s0,s1)>>1) ^ (-
loBit(s1) & 0x9908b0dfUL); }
142 {
seed(bigSeed,seedLength); }
148 {
return double(
randInt()) * (1.0/4294967295.0); }
151 {
return rand() * n; }
154 {
return double(
randInt()) * (1.0/4294967296.0); }
160 {
return (
double(
randInt()) + 0.5 ) * (1.0/4294967296.0); }
168 return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0);
176 double phi = 2.0 * 3.14159265358979323846264338328 *
randExc();
177 return mean + r * cos(phi);
191 s1 ^= (s1 << 7) & 0x9d2c5680UL;
192 s1 ^= (s1 << 15) & 0xefc60000UL;
193 return ( s1 ^ (s1 >> 18) );
235 register int k = (
N > seedLength ?
N : seedLength );
240 state[i] += ( bigSeed[j] & 0xffffffffUL ) + j;
241 state[i] &= 0xffffffffUL;
244 if( j >= seedLength ) j = 0;
246 for( k =
N - 1; k; --k )
251 state[i] &= 0xffffffffUL;
255 state[0] = 0x80000000UL;
267 FILE* urandom = fopen(
"/dev/urandom",
"rb" );
271 register uint32 *s = bigSeed;
273 register bool success =
true;
274 while( success && i-- )
275 success = (fread( s++,
sizeof(
uint32), 1, urandom ) > 0);
277 if( success ) {
seed( bigSeed,
N );
return; }
295 *s++ =
seed & 0xffffffffUL;
298 *s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL;
310 for( i =
N -
M; i--; ++p )
311 *p =
twist( p[
M], p[0], p[1] );
312 for( i =
M; --i; ++p )
313 *p =
twist( p[
M-
N], p[0], p[1] );
329 unsigned char *p = (
unsigned char *) &t;
330 for(
size_t i = 0; i <
sizeof(t); ++i )
332 h1 *= UCHAR_MAX + 2U;
336 p = (
unsigned char *) &c;
337 for(
size_t j = 0; j <
sizeof(c); ++j )
339 h2 *= UCHAR_MAX + 2U;
342 return ( h1 + differ++ ) ^ h2;
348 register uint32 *sa = saveArray;
351 for( ; i--; *sa++ = *s++ ) {}
359 register uint32 *la = loadArray;
361 for( ; i--; *s++ = *la++ ) {}
370 register int i = mtrand.
N;
371 for( ; i--; os << *s++ <<
"\t" ) {}
372 return os << mtrand.
left;
379 register int i = mtrand.
N;
380 for( ; i--; is >> *s++ ) {}
std::ostream & operator<<(std::ostream &os, const MTRand &mtrand)
Definition: MersenneTwister.h:367
std::istream & operator>>(std::istream &is, MTRand &mtrand)
Definition: MersenneTwister.h:376
Definition: MersenneTwister.h:70
friend std::ostream & operator<<(std::ostream &os, const MTRand &mtrand)
Definition: MersenneTwister.h:367
@ N
Definition: MersenneTwister.h:75
uint32 twist(const uint32 &m, const uint32 &s0, const uint32 &s1) const
Definition: MersenneTwister.h:132
uint32 mixBits(const uint32 &u, const uint32 &v) const
Definition: MersenneTwister.h:130
void save(uint32 *saveArray) const
Definition: MersenneTwister.h:346
friend std::istream & operator>>(std::istream &is, MTRand &mtrand)
Definition: MersenneTwister.h:376
uint32 state[N]
Definition: MersenneTwister.h:81
uint32 loBit(const uint32 &u) const
Definition: MersenneTwister.h:128
unsigned long uint32
Definition: MersenneTwister.h:73
double randDblExc()
Definition: MersenneTwister.h:159
uint32 loBits(const uint32 &u) const
Definition: MersenneTwister.h:129
void initialize(const uint32 oneSeed)
Definition: MersenneTwister.h:286
static uint32 hash(time_t t, clock_t c)
Definition: MersenneTwister.h:320
void load(uint32 *const loadArray)
Definition: MersenneTwister.h:356
double rand()
Definition: MersenneTwister.h:147
void seed()
Definition: MersenneTwister.h:260
uint32 * pNext
Definition: MersenneTwister.h:82
MTRand()
Definition: MersenneTwister.h:144
double operator()()
Definition: MersenneTwister.h:105
uint32 randInt()
Definition: MersenneTwister.h:180
@ M
Definition: MersenneTwister.h:79
uint32 hiBit(const uint32 &u) const
Definition: MersenneTwister.h:127
int left
Definition: MersenneTwister.h:83
@ SAVE
Definition: MersenneTwister.h:76
double randExc()
Definition: MersenneTwister.h:153
double rand53()
Definition: MersenneTwister.h:165
double randNorm(const double &mean=0.0, const double &variance=0.0)
Definition: MersenneTwister.h:171
void reload()
Definition: MersenneTwister.h:304
T mean(const std::vector< T > &array, int start, int end)
Definition: essentiamath.h:143
T variance(const std::vector< T > &array, const T mean)
Definition: essentiamath.h:467
#define NULL
Definition: tnt_i_refvec.h:33
uint32_t uint32
Definition: types.h:43