51#ifndef MERSENNETWISTER_H
52#define MERSENNETWISTER_H
112 {
seed(bigSeed,seedLength); }
119 inline double rand() {
return double(
randInt()) * (1.0/4294967295.0); }
121 inline double rand(
const double& n ) {
return rand() * n; }
143 s1 ^= (s1 << 7) & 0x9d2c5680UL;
144 s1 ^= (s1 << 15) & 0xefc60000UL;
145 return ( s1 ^ (s1 >> 18) );
178 return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0);
184 inline double randNorm(
const double& mean,
const double& variance )
186 double r = sqrt( -2.0 * log( 1.0-
randDblExc()) ) * variance;
187 double phi = STK::Const::_2PI_ *
randExc();
188 return mean + r * cos(phi);
212 int k = (
N > seedLength ?
N : seedLength );
217 state[i] += ( bigSeed[j] & 0xffffffffUL ) + j;
218 state[i] &= 0xffffffffUL;
221 if( j >= seedLength ) j = 0;
223 for( k =
N - 1; k; --k )
228 state[i] &= 0xffffffffUL;
232 state[0] = 0x80000000UL;
243 FILE* urandom = fopen(
"/dev/urandom",
"rb" );
250 while( success && i-- )
251 success = fread( s++,
sizeof(
uint32), 1, urandom );
253 if( success ) {
seed( bigSeed,
N );
return; }
257 seed(
hash( time(NULL), clock() ) );
268 for( ; i--; *sa++ = *s++ ) {}
280 for( ; i--; *s++ = *la++ ) {}
297 *s++ =
seed & 0xffffffffUL;
300 *s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL;
313 for( i =
N -
M; i--; ++p )
314 *p =
twist( p[
M], p[0], p[1] );
315 for( i =
M; --i; ++p )
316 *p =
twist( p[
M-
N], p[0], p[1] );
323 {
return u & 0x80000000UL; }
327 {
return u & 0x00000001UL; }
331 {
return u & 0x7fffffffUL; }
339 {
return m ^ (
mixBits(s0,s1)>>1) ^ (-
loBit(s1) & 0x9908b0dfUL); }
350 unsigned char *p = (
unsigned char *) &t;
351 for(
size_t i = 0; i <
sizeof(t); ++i )
353 h1 *= UCHAR_MAX + 2U;
357 p = (
unsigned char *) &c;
358 for(
size_t j = 0; j <
sizeof(c); ++j )
360 h2 *= UCHAR_MAX + 2U;
363 return ( h1 + differ++ ) ^ h2;
In this file we give the main mathematical constants.
Generate unsigner 32 bits random number using the Merdsenne Twister Algorithm.
double rand53()
Access to 53-bit random numbers (capacity of IEEE double precision).
uint32 loBits(const uint32 &u) const
low bits.
void reload()
Generate N new values in state Made disposeer and faster by Matthew Bellew (matthew....
void seed(const uint32 oneSeed)
Re-seeding functions with same behavior as initializers.
MTRand()
auto-initialize with /dev/urandom or time() and clock()
double randExc(const double &n)
real number in [0,n)
uint32 * pNext
Next value to get from state.
uint32 state[N]
internal state
MTRand(uint32 *const bigSeed, uint32 const seedLength=N)
Initialize with a an array.
static uint32 hash(time_t t, clock_t c)
Get a uint32 from t and c Better than uint32(x) in case x is floating point in [0,...
uint32 mixBits(const uint32 &u, const uint32 &v) const
mixed bits.
unsigned long uint32
unsigned integer type, at least 32 bits
double randDblExc()
real number in (0,1)
double randDblExc(const double &n)
real number in (0,n)
uint32 randInt(const uint32 &n)
integer in [0,n] for n < 2^32
uint32 loBit(const uint32 &u) const
low bit.
void seed(uint32 *const bigSeed, const uint32 seedLength)
Re-seeding functions with same behavior as initializers.
double rand()
real number in [0,1]
double rand(const double &n)
real number in [0,n]
void load(uint32 *const loadArray)
Saving and loading generator state.
uint32 randInt()
integer in [0,2^32-1]
uint32 hiBit(const uint32 &u) const
High bit.
int left
number of values left before reload needed
void save(uint32 *saveArray) const
Saving and loading generator state.
MTRand(const uint32 &oneSeed)
Initialize with a simple uint32.
double operator()()
same as rand().
double randNorm(const double &mean, const double &variance)
Access to nonuniform random number distributions.
void seed()
Re-seeding functions with same behavior as initializers.
void initialize(const uint32 seed)
Initialize generator state with seed See Knuth TAOCP Vol 2, 3rd Ed, p.106 for multiplier.
uint32 twist(const uint32 &m, const uint32 &s0, const uint32 &s1) const
twisted values.
double randExc()
real number in [0,1)