65 #ifndef VL_MERSENNETWISTER_H 66 #define VL_MERSENNETWISTER_H 81 class MersenneTwister;
115 uint32
randInt(
const uint32 n );
117 double rand(
const double n );
119 double randExc(
const double n );
128 double randNorm(
const double mean = 0.0,
const double stddev = 1.0 );
131 void seed(
const uint32 oneSeed );
132 void seed( uint32 *
const bigSeed,
const uint32 seedLength =
N );
136 void save( uint32* saveArray )
const;
137 void load( uint32 *
const loadArray );
145 uint32
hiBit(
const uint32 u )
const {
return u & 0x80000000UL; }
146 uint32
loBit(
const uint32 u )
const {
return u & 0x00000001UL; }
147 uint32
loBits(
const uint32 u )
const {
return u & 0x7fffffffUL; }
148 uint32
mixBits(
const uint32 u,
const uint32 v )
const 150 uint32
magic(
const uint32 u )
const 151 {
return loBit(u) ? 0x9908b0dfUL : 0x0UL; }
152 uint32
twist(
const uint32 m,
const uint32 s0,
const uint32 s1 )
const 154 static uint32
hash( time_t t, clock_t c );
168 unsigned char *p = (
unsigned char *) &t;
169 for(
size_t i = 0; i <
sizeof(t); ++i )
171 h1 *= UCHAR_MAX + 2U;
175 p = (
unsigned char *) &c;
176 for(
size_t j = 0; j <
sizeof(c); ++j )
178 h2 *= UCHAR_MAX + 2U;
181 return ( h1 + differ++ ) ^ h2;
193 *s++ = seed & 0xffffffffUL;
196 *s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL;
205 static const int MmN = int(
M) - int(
N);
208 for( i =
N -
M; i--; ++p )
209 *p =
twist( p[
M], p[0], p[1] );
210 for( i = M; --i; ++p )
211 *p =
twist( p[MmN], p[0], p[1] );
235 register int k = (
N > seedLength ? (int)
N : (
int)seedLength );
240 state[i] += ( bigSeed[j] & 0xffffffffUL ) + j;
241 state[i] &= 0xffffffffUL;
243 if( i >=
N ) { state[0] = state[
N-1]; i = 1; }
244 if( j >= seedLength ) j = 0;
246 for( k =
N - 1; k; --k )
251 state[i] &= 0xffffffffUL;
253 if( i >=
N ) { state[0] = state[
N-1]; i = 1; }
255 state[0] = 0x80000000UL;
265 FILE* urandom = fopen(
"/dev/urandom",
"rb" );
269 register uint32 *s = bigSeed;
271 register bool success =
true;
272 while( success && i-- )
273 success = fread( s++,
sizeof(
uint32), 1, urandom ) != 0;
275 if( success ) {
seed( bigSeed,
N );
return; }
286 {
seed(bigSeed,seedLength); }
296 for( ; i--; *s++ = *t++ ) {}
312 s1 ^= (s1 << 7) & 0x9d2c5680UL;
313 s1 ^= (s1 << 15) & 0xefc60000UL;
314 return ( s1 ^ (s1 >> 18) );
337 {
return double(
randInt()) * (1.0/4294967295.0); }
340 {
return rand() * n; }
343 {
return double(
randInt()) * (1.0/4294967296.0); }
349 {
return (
double(
randInt()) + 0.5 ) * (1.0/4294967296.0); }
357 return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0);
367 x = 2.0 *
rand() - 1.0;
368 y = 2.0 *
rand() - 1.0;
371 while ( r >= 1.0 || r == 0.0 );
372 double s =
sqrt( -2.0 *
log(r) / r );
373 return mean + x * s * stddev;
384 register uint32 *sa = saveArray;
386 for( ; i--; *sa++ = *s++ ) {}
393 register uint32 *la = loadArray;
395 for( ; i--; *s++ = *la++ ) {}
403 register int i = mtrand.
N;
404 for( ; i--; os << *s++ <<
"\t" ) {}
405 return os << mtrand.
left;
411 register int i = mtrand.
N;
412 for( ; i--; is >> *s++ ) {}
420 if(
this == &o )
return (*
this);
424 for( ; i--; *s++ = *t++ ) {}
432 #endif // MERSENNETWISTER_H
uint32 loBits(const uint32 u) const
static uint32 hash(time_t t, clock_t c)
void load(uint32 *const loadArray)
uint32 hiBit(const uint32 u) const
MersenneTwister & operator=(const MersenneTwister &o)
Visualization Library main namespace.
uint32 magic(const uint32 u) const
void initialize(const uint32 oneSeed)
uint32 twist(const uint32 m, const uint32 s0, const uint32 s1) const
double randNorm(const double mean=0.0, const double stddev=1.0)
The base class for all the reference counted objects.
uint32 mixBits(const uint32 u, const uint32 v) const
void save(uint32 *saveArray) const
VLCORE_EXPORT void setDefMersenneTwister(MersenneTwister *)
friend std::ostream & operator<<(std::ostream &os, const MersenneTwister &mtrand)
VLCORE_EXPORT MersenneTwister * defMersenneTwister()
uint32 loBit(const uint32 u) const
friend std::istream & operator>>(std::istream &is, MersenneTwister &mtrand)