57 #ifndef __OMNETPP_MERSENNETWISTER_H 58 #define __OMNETPP_MERSENNETWISTER_H 74 typedef unsigned long uint32;
76 enum Dummy1 { N = 624 };
77 enum Dummy2 { SAVE = N + 1 };
81 enum Dummy3 { M = 397 };
90 MTRand(
const uint32& oneSeed );
91 MTRand( uint32 *
const bigSeed, uint32
const seedLength = N );
100 double rand(
const double& n );
102 double randExc(
const double& n );
104 double randDblExc(
const double& n );
106 uint32 randInt(
const uint32& n );
107 double operator()() {
return rand(); }
113 double randNorm(
const double& mean = 0.0,
const double& variance = 0.0 );
116 void seed(
const uint32 oneSeed );
117 void seed( uint32 *
const bigSeed,
const uint32 seedLength = N );
121 void save( uint32* saveArray )
const;
122 void load( uint32 *
const loadArray );
123 friend std::ostream& operator<<( std::ostream& os,
const MTRand& mtrand );
124 friend std::istream& operator>>( std::istream& is, MTRand& mtrand );
127 void initialize(
const uint32 oneSeed );
129 uint32 hiBit(
const uint32& u )
const {
return u & 0x80000000UL; }
130 uint32 loBit(
const uint32& u )
const {
return u & 0x00000001UL; }
131 uint32 loBits(
const uint32& u )
const {
return u & 0x7fffffffUL; }
132 uint32 mixBits(
const uint32& u,
const uint32& v )
const 133 {
return hiBit(u) | loBits(v); }
134 uint32 twist(
const uint32& m,
const uint32& s0,
const uint32& s1 )
const 135 {
return m ^ (mixBits(s0,s1)>>1) ^ (-loBit(s1) & 0x9908b0dfUL); }
136 static uint32 hash( time_t t, clock_t c );
140 inline MTRand::MTRand(
const uint32& oneSeed )
143 inline MTRand::MTRand( uint32 *
const bigSeed,
const uint32 seedLength )
144 { seed(bigSeed,seedLength); }
146 inline MTRand::MTRand()
149 inline double MTRand::rand()
150 {
return double(randInt()) * (1.0/4294967295.0); }
152 inline double MTRand::rand(
const double& n )
153 {
return rand() * n; }
155 inline double MTRand::randExc()
156 {
return double(randInt()) * (1.0/4294967296.0); }
158 inline double MTRand::randExc(
const double& n )
159 {
return randExc() * n; }
161 inline double MTRand::randDblExc()
162 {
return (
double(randInt()) + 0.5 ) * (1.0/4294967296.0); }
164 inline double MTRand::randDblExc(
const double& n )
165 {
return randDblExc() * n; }
167 inline double MTRand::rand53()
169 uint32 a = randInt() >> 5, b = randInt() >> 6;
170 return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0);
173 inline double MTRand::randNorm(
const double& mean,
const double& variance )
177 double r = sqrt( -2.0 * log( 1.0-randDblExc()) ) * variance;
178 double phi = 2.0 * 3.14159265358979323846264338328 * randExc();
179 return mean + r * cos(phi);
182 inline MTRand::uint32 MTRand::randInt()
187 if( left == 0 ) reload();
193 s1 ^= (s1 << 7) & 0x9d2c5680UL;
194 s1 ^= (s1 << 15) & 0xefc60000UL;
195 return ( s1 ^ (s1 >> 18) );
198 inline MTRand::uint32 MTRand::randInt(
const uint32& n )
212 i = randInt() & used;
218 inline void MTRand::seed(
const uint32 oneSeed )
226 inline void MTRand::seed( uint32 *
const bigSeed,
const uint32 seedLength )
234 initialize(19650218UL);
237 int k = ( N > seedLength ? N : seedLength );
241 state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1664525UL );
242 state[i] += ( bigSeed[j] & 0xffffffffUL ) + j;
243 state[i] &= 0xffffffffUL;
245 if( i >= N ) { state[0] = state[N-1]; i = 1; }
246 if( j >= seedLength ) j = 0;
248 for( k = N - 1; k; --k )
251 state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1566083941UL );
253 state[i] &= 0xffffffffUL;
255 if( i >= N ) { state[0] = state[N-1]; i = 1; }
257 state[0] = 0x80000000UL;
262 inline void MTRand::seed()
268 FILE* urandom = fopen(
"/dev/urandom",
"rb" );
275 while( success && i-- )
276 success = fread( s++,
sizeof(uint32), 1, urandom );
278 if( success ) { seed( bigSeed, N );
return; }
282 seed( hash( time(
nullptr), clock() ) );
286 inline void MTRand::initialize(
const uint32 seed )
295 *s++ = seed & 0xffffffffUL;
298 *s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL;
304 inline void MTRand::reload()
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] );
314 *p = twist( p[M-N], p[0], state[0] );
316 left = N, pNext = state;
320 inline MTRand::uint32 MTRand::hash( time_t t, clock_t c )
326 static uint32 differ = 0;
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;
346 inline void MTRand::save( uint32* saveArray )
const 348 uint32 *sa = saveArray;
349 const uint32 *s = state;
351 for( ; i--; *sa++ = *s++ ) {}
356 inline void MTRand::load( uint32 *
const loadArray )
359 uint32 *la = loadArray;
361 for( ; i--; *s++ = *la++ ) {}
363 pNext = &state[N-left];
367 inline std::ostream& operator<<( std::ostream& os,
const MTRand& mtrand )
369 const MTRand::uint32 *s = mtrand.state;
371 for( ; i--; os << *s++ <<
"\t" ) {}
372 return os << mtrand.left;
376 inline std::istream& operator>>( std::istream& is, MTRand& mtrand )
378 MTRand::uint32 *s = mtrand.state;
380 for( ; i--; is >> *s++ ) {}
382 mtrand.pNext = &mtrand.state[mtrand.N-mtrand.left];
389 #endif // MERSENNETWISTER_H Definition: cabstracthistogram.h:21