00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __VCG_RandomGenerator
00025 #define __VCG_RandomGenerator
00026
00027 #include <vcg/math/base.h>
00028
00029 namespace vcg {
00030 namespace math {
00031
00037 class RandomGenerator
00038 {
00039
00040
00041 public:
00042
00043 RandomGenerator(){}
00044
00045 virtual ~RandomGenerator()
00046 {}
00047
00048
00049 public:
00050
00052 virtual void initialize(unsigned int seed)=0;
00053
00055 virtual unsigned int generate(unsigned int limit)=0;
00056
00058 virtual double generate01()=0;
00059
00061 virtual double generate01closed()=0;
00062
00064 virtual double generate01open()=0;
00065 };
00066
00079 class SubtractiveRingRNG : public RandomGenerator
00080 {
00081
00082
00083 private:
00084
00085
00086 unsigned int _M_table[55];
00087 size_t _M_index1;
00088 size_t _M_index2;
00089
00090
00091 public:
00092
00093
00094 SubtractiveRingRNG(int default_seed=161803398u)
00095 {
00096 initialize(default_seed);
00097 }
00098
00099 virtual ~SubtractiveRingRNG()
00100 {}
00101
00102
00103 public:
00104
00106 void initialize(unsigned int seed)
00107 {
00108 unsigned int __k = 1;
00109 _M_table[54] = seed;
00110 size_t __i;
00111 for (__i = 0; __i < 54; __i++)
00112 {
00113 size_t __ii = (21 * (__i + 1) % 55) - 1;
00114 _M_table[__ii] = __k;
00115 __k = seed - __k;
00116 seed = _M_table[__ii];
00117 }
00118 for (int __loop = 0; __loop < 4; __loop++)
00119 {
00120 for (__i = 0; __i < 55; __i++)
00121 _M_table[__i] = _M_table[__i] - _M_table[(1 + __i + 30) % 55];
00122 }
00123 _M_index1 = 0;
00124 _M_index2 = 31;
00125 }
00126
00128 unsigned int generate(unsigned int limit= 0xffffffffu)
00129 {
00130 _M_index1 = (_M_index1 + 1) % 55;
00131 _M_index2 = (_M_index2 + 1) % 55;
00132 _M_table[_M_index1] = _M_table[_M_index1] - _M_table[_M_index2];
00133 return _M_table[_M_index1] % limit;
00134 }
00135
00137 double generate01()
00138 {
00139 const unsigned int lmt = 0xffffffffu;
00140 unsigned int number = generate(lmt);
00141 return static_cast<double>(number) / static_cast<double>(lmt);
00142 }
00143
00145 double generate01closed()
00146 {
00147 const unsigned int lmt = 0xffffffffu;
00148 unsigned int number = generate(lmt);
00149 return static_cast<double>(number) / static_cast<double>(0xfffffffEu);
00150 }
00151
00153 double generate01open()
00154 {
00155 const unsigned int lmt = 0xffffffffu;
00156 unsigned int number = generate(lmt);
00157 return (static_cast<double>(number) + 0.5) * (1.0/static_cast<double>(lmt));
00158 }
00159
00160 };
00161
00173 class MarsenneTwisterRNG : public RandomGenerator
00174 {
00175
00176
00177 private:
00178
00179 static const int N = 624;
00180 static const int M = 397;
00181 static const unsigned int MATRIX_A = 0x9908b0dfu;
00182 static const unsigned int UPPER_MASK = 0x80000000u;
00183 static const unsigned int LOWER_MASK = 0x7fffffffu;
00184
00185
00186 private:
00187
00188
00189 unsigned int mt[N];
00190 int mti;
00191
00192
00193 public:
00194
00195
00196 MarsenneTwisterRNG()
00197 {
00198 initialize(5489u);
00199 }
00200
00201 virtual ~MarsenneTwisterRNG()
00202 {}
00203
00204
00205
00206 public:
00207
00209 void initialize(unsigned int seed)
00210 {
00211 mt[0]= seed & 0xffffffffu;
00212 for (mti=1; mti<N; mti++)
00213 {
00214 mt[mti] = (1812433253u * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
00215
00216
00217
00218
00219 mt[mti] &= 0xffffffffu;
00220
00221 }
00222 }
00223
00230 void initializeByArray(unsigned int init_key[], int key_length)
00231 {
00232 int i, j, k;
00233 initialize(19650218u);
00234 i=1; j=0;
00235 k = (N>key_length ? N : key_length);
00236 for (; k; k--)
00237 {
00238 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525u)) + init_key[j] + j;
00239 mt[i] &= 0xffffffffu;
00240 i++; j++;
00241
00242 if (i>=N)
00243 {
00244 mt[0] = mt[N-1];
00245 i=1;
00246 }
00247
00248 if (j>=key_length) j=0;
00249 }
00250
00251 for (k=N-1; k; k--)
00252 {
00253 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941u)) - i;
00254 mt[i] &= 0xffffffffu;
00255 i++;
00256 if (i>=N)
00257 {
00258 mt[0] = mt[N-1];
00259 i=1;
00260 }
00261 }
00262
00263 mt[0] = 0x80000000u;
00264 }
00265
00271 unsigned int generate(unsigned int )
00272 {
00273 unsigned int y;
00274 static unsigned int mag01[2]={0x0u, MATRIX_A};
00275
00276
00277 if (mti >= N)
00278 {
00279 int kk;
00280
00281 for (kk=0;kk<N-M;kk++)
00282 {
00283 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
00284 mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1u];
00285 }
00286
00287 for (;kk<N-1;kk++)
00288 {
00289 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
00290 mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1u];
00291 }
00292
00293 y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
00294 mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1u];
00295
00296 mti = 0;
00297 }
00298
00299 y = mt[mti++];
00300
00301
00302 y ^= (y >> 11);
00303 y ^= (y << 7) & 0x9d2c5680u;
00304 y ^= (y << 15) & 0xefc60000u;
00305 y ^= (y >> 18);
00306
00307 return y;
00308 }
00309
00311 double generate01closed()
00312 {
00313 return generate(0)*(1.0/4294967295.0);
00314 }
00315
00317 double generate01()
00318 {
00319 return generate(0)*(1.0/4294967296.0);
00320 }
00321
00323 double generate01open()
00324 {
00325 return (((double)generate(0)) + 0.5)*(1.0/4294967296.0);
00326 }
00327
00328 };
00329
00330
00331
00332
00333
00334
00335
00336
00337 inline double box_muller(RandomGenerator &generator, double m, double s)
00338 {
00339 double x1, x2, w, y1;
00340 static double y2;
00341 static int use_last = 0;
00342 static RandomGenerator *last_generator = 0;
00343 if(last_generator != &generator)
00344 use_last = 0;
00345 last_generator = &generator;
00346 if (use_last){
00347 y1 = y2;
00348 use_last = 0;
00349 } else {
00350 do {
00351 x1 = 2.0 * generator.generate01closed() - 1.0;
00352 x2 = 2.0 * generator.generate01closed() - 1.0;
00353 w = x1 * x1 + x2 * x2;
00354 } while ( w >= 1.0 );
00355 w = sqrt( (-2.0 * log( w ) ) / w );
00356 y1 = x1 * w;
00357 y2 = x2 * w;
00358 use_last = 1;
00359 }
00360 return( m + y1 * s );
00361 }
00362 }
00363 }
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400 #endif