00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 #ifndef MERSENNETWISTER_WFMATH_H
00061 #define MERSENNETWISTER_WFMATH_H
00062
00063
00064
00065
00066 #include <iosfwd>
00067 #include <climits>
00068 #include <cmath>
00069
00070
00071
00072 namespace WFMath {
00073
00074 class MTRand {
00075
00076 public:
00077 typedef unsigned long uint32;
00078
00079 static const uint32 N = 624;
00080 static const uint32 SAVE = N + 1;
00081
00082 protected:
00083 static const uint32 M = 397;
00084
00085 uint32 state[N];
00086 uint32 *pNext;
00087 int left;
00088
00089
00090
00091 public:
00092 MTRand( const uint32& oneSeed );
00093 MTRand( uint32 *const bigSeed, uint32 const seedLength = N );
00094 MTRand();
00095
00096
00097
00098
00099
00100
00101 template<typename FloatT>
00102 FloatT rand();
00103 float randf();
00104 float randf( const float& n );
00105 double rand();
00106 double rand( const double& n );
00107 double randExc();
00108 double randExc( const double& n );
00109 double randDblExc();
00110 double randDblExc( const double& n );
00111 uint32 randInt();
00112 uint32 randInt( const uint32& n );
00113 double operator()() { return rand(); }
00114
00115
00116 double rand53();
00117
00118
00119 double randNorm( const double& mean = 0.0, const double& variance = 0.0 );
00120
00121
00122 void seed( const uint32 oneSeed );
00123 void seed( uint32 *const bigSeed, const uint32 seedLength = N );
00124 void seed();
00125
00126
00127 void save( uint32* saveArray ) const;
00128 void load( uint32 *const loadArray );
00129 friend std::ostream& operator<<( std::ostream& os, const MTRand& mtrand );
00130 friend std::istream& operator>>( std::istream& is, MTRand& mtrand );
00131
00132 static MTRand instance;
00133
00134 protected:
00135 void initialize( const uint32 oneSeed );
00136 void reload();
00137 uint32 hiBit( const uint32& u ) const { return u & 0x80000000UL; }
00138 uint32 loBit( const uint32& u ) const { return u & 0x00000001UL; }
00139 uint32 loBits( const uint32& u ) const { return u & 0x7fffffffUL; }
00140 uint32 mixBits( const uint32& u, const uint32& v ) const
00141 { return hiBit(u) | loBits(v); }
00142 uint32 twist( const uint32& m, const uint32& s0, const uint32& s1 ) const
00143 { return m ^ (mixBits(s0,s1)>>1) ^ (-loBit(s1) & 0x9908b0dfUL); }
00144 };
00145
00146
00147 inline MTRand::MTRand( const uint32& oneSeed ) : pNext(0), left(0)
00148 { seed(oneSeed); }
00149
00150 inline MTRand::MTRand( uint32 *const bigSeed, const uint32 seedLength ) : pNext(0), left(0)
00151 { seed(bigSeed,seedLength); }
00152
00153 inline MTRand::MTRand() : pNext(0), left(0)
00154 { seed(); }
00155
00156 template<>
00157 inline float MTRand::rand<float>()
00158 { return float(randInt()) * (1.0f/4294967295.0f); }
00159
00160 template<>
00161 inline double MTRand::rand<double>()
00162 { return double(randInt()) * (1.0/4294967295.0); }
00163
00164 inline float MTRand::randf()
00165 { return float(randInt()) * (1.0f/4294967295.0f); }
00166
00167 inline float MTRand::randf( const float& n )
00168 { return randf() * n; }
00169
00170 inline double MTRand::rand()
00171 { return double(randInt()) * (1.0/4294967295.0); }
00172
00173 inline double MTRand::rand( const double& n )
00174 { return rand() * n; }
00175
00176 inline double MTRand::randExc()
00177 { return double(randInt()) * (1.0/4294967296.0); }
00178
00179 inline double MTRand::randExc( const double& n )
00180 { return randExc() * n; }
00181
00182 inline double MTRand::randDblExc()
00183 { return ( double(randInt()) + 0.5 ) * (1.0/4294967296.0); }
00184
00185 inline double MTRand::randDblExc( const double& n )
00186 { return randDblExc() * n; }
00187
00188 inline double MTRand::rand53()
00189 {
00190 uint32 a = randInt() >> 5, b = randInt() >> 6;
00191 return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0);
00192 }
00193
00194 inline double MTRand::randNorm( const double& mean, const double& variance )
00195 {
00196
00197
00198 double r = std::sqrt( -2.0 * std::log( 1.0-randDblExc()) ) * variance;
00199 double phi = 2.0 * 3.14159265358979323846264338328 * randExc();
00200 return mean + r * std::cos(phi);
00201 }
00202
00203 inline MTRand::uint32 MTRand::randInt()
00204 {
00205
00206
00207
00208 if( left == 0 ) reload();
00209 --left;
00210
00211 register uint32 s1;
00212 s1 = *pNext++;
00213 s1 ^= (s1 >> 11);
00214 s1 ^= (s1 << 7) & 0x9d2c5680UL;
00215 s1 ^= (s1 << 15) & 0xefc60000UL;
00216 return ( s1 ^ (s1 >> 18) );
00217 }
00218
00219 inline MTRand::uint32 MTRand::randInt( const uint32& n )
00220 {
00221
00222
00223 uint32 used = n;
00224 used |= used >> 1;
00225 used |= used >> 2;
00226 used |= used >> 4;
00227 used |= used >> 8;
00228 used |= used >> 16;
00229
00230
00231 uint32 i;
00232 do
00233 i = randInt() & used;
00234 while( i > n );
00235 return i;
00236 }
00237
00238
00239 inline void MTRand::seed( const uint32 oneSeed )
00240 {
00241
00242 initialize(oneSeed);
00243 reload();
00244 }
00245
00246
00247 inline void MTRand::seed( uint32 *const bigSeed, const uint32 seedLength )
00248 {
00249
00250
00251
00252
00253
00254
00255 initialize(19650218UL);
00256 register unsigned i = 1;
00257 register uint32 j = 0;
00258 register unsigned k = ( N > seedLength ? N : seedLength );
00259 for( ; k; --k )
00260 {
00261 state[i] =
00262 state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1664525UL );
00263 state[i] += ( bigSeed[j] & 0xffffffffUL ) + j;
00264 state[i] &= 0xffffffffUL;
00265 ++i; ++j;
00266 if( i >= N ) { state[0] = state[N-1]; i = 1; }
00267 if( j >= seedLength ) j = 0;
00268 }
00269 for( k = N - 1; k; --k )
00270 {
00271 state[i] =
00272 state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1566083941UL );
00273 state[i] -= i;
00274 state[i] &= 0xffffffffUL;
00275 ++i;
00276 if( i >= N ) { state[0] = state[N-1]; i = 1; }
00277 }
00278 state[0] = 0x80000000UL;
00279 reload();
00280 }
00281
00282
00283 inline void MTRand::initialize( const uint32 seed )
00284 {
00285
00286
00287
00288
00289 register uint32 *s = state;
00290 register uint32 *r = state;
00291 register unsigned i = 1;
00292 *s++ = seed & 0xffffffffUL;
00293 for( ; i < N; ++i )
00294 {
00295 *s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL;
00296 r++;
00297 }
00298 }
00299
00300
00301 inline void MTRand::reload()
00302 {
00303
00304
00305 register uint32 *p = state;
00306 register int i;
00307 for( i = N - M; i--; ++p )
00308 *p = twist( p[M], p[0], p[1] );
00309 for( i = M; --i; ++p )
00310 *p = twist( p[M-N], p[0], p[1] );
00311 *p = twist( p[M-N], p[0], state[0] );
00312
00313 left = N, pNext = state;
00314 }
00315
00316
00317
00318 inline void MTRand::save( uint32* saveArray ) const
00319 {
00320 register uint32 *sa = saveArray;
00321 register const uint32 *s = state;
00322 register int i = N;
00323 for( ; i--; *sa++ = *s++ ) {}
00324 *sa = left;
00325 }
00326
00327
00328 inline void MTRand::load( uint32 *const loadArray )
00329 {
00330 register uint32 *s = state;
00331 register uint32 *la = loadArray;
00332 register int i = N;
00333 for( ; i--; *s++ = *la++ ) {}
00334 left = *la;
00335 pNext = &state[N-left];
00336 }
00337
00338
00339 }
00340
00341 #endif // MERSENNETWISTER_H
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381