Go to the documentation of this file.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
00054 #include "fvar.hpp"
00055
00056 #define N 624
00057 #define M 397
00058 #define MATRIX_A 0x9908b0dfUL
00059 #define UPPER_MASK 0x80000000UL
00060 #define LOWER_MASK 0x7fffffffUL
00061
00070 random_number_generator::random_number_generator(const int seed)
00071 {
00072 unsigned long s=seed;
00073 mt=new unsigned long [N];
00074 mti=N+1;
00075
00076 mt[0]= s & 0xffffffffUL;
00077 for (mti=1; mti<N; mti++) {
00078 mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
00079
00080
00081
00082
00083 mt[mti] &= 0xffffffffUL;
00084
00085 }
00086 better_rand();
00087 }
00088
00092 random_number_generator::~random_number_generator()
00093 {
00094 delete [] mt;
00095 mt = 0;
00096 }
00097
00106 void random_number_generator::reinitialize(int seed)
00107 {
00108 unsigned long s=seed;
00109 mt[0]= s & 0xffffffffUL;
00110 for (mti=1; mti<N; mti++) {
00111 mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
00112
00113
00114
00115
00116 mt[mti] &= 0xffffffffUL;
00117
00118 }
00119 better_rand();
00120 }
00121
00134 double random_number_generator::better_rand()
00135 {
00136 unsigned long y;
00137 static unsigned long mag01[2]={0x0UL, MATRIX_A};
00138
00139
00140 if (mti >= N) {
00141 int kk = 0;
00142
00143
00144
00145
00146 for (;kk<N-M;kk++) {
00147 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
00148 mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
00149 }
00150 for (;kk<N-1;kk++) {
00151 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
00152 mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
00153 }
00154 y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
00155 mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
00156
00157 mti = 0;
00158 }
00159
00160 y = mt[mti++];
00161
00162
00163 y ^= (y >> 11);
00164 y ^= (y << 7) & 0x9d2c5680UL;
00165 y ^= (y << 15) & 0xefc60000UL;
00166 y ^= (y >> 18);
00167
00168 return (((double)y) + 0.5)*(1.0/4294967296.0);
00169 }
00170
00171 #undef N
00172 #undef M
00173 #undef MATRIX_A
00174 #undef UPPER_MASK
00175 #undef LOWER_MASK
00176
00183 double randn(const random_number_generator& rng)
00184 {
00185 double x=((random_number_generator&) rng).better_rand();
00186 double y=((random_number_generator&) rng).better_rand();
00187 double u=sqrt(-2*log(x))*cos(2*PI*y);
00188 return u;
00189 }
00190
00198 double randu(const random_number_generator& rng)
00199 {
00200 return ((random_number_generator&)rng).better_rand();
00201 }
00202
00207 void dvector::fill_randbi(double p, const random_number_generator& rng)
00208 {
00209 if ( p<0 || p>1)
00210 {
00211 cerr << "Error in dvar_vector::fill_randbi proportions of"
00212 " successes must lie between 0 and 1\n";
00213 ad_exit(1);
00214 }
00215 for (int i=indexmin(); i<=indexmax(); i++)
00216 {
00217 if ( ((random_number_generator&) rng).better_rand()<=p)
00218 {
00219 elem(i)=1;
00220 }
00221 else
00222 {
00223 elem(i)=0;
00224 }
00225 }
00226 }
00227
00232 void dvector::fill_randu(const random_number_generator& rng)
00233 {
00234 for (int i=indexmin(); i<=indexmax(); i++)
00235 {
00236 elem(i)=((random_number_generator&) rng).better_rand();
00237 }
00238 }
00239
00244 void dmatrix::colfill_randu(const int&j, const random_number_generator& rng)
00245 {
00246 for (int i=rowmin(); i<=rowmax(); i++)
00247 {
00248 elem(i,j)=((random_number_generator&) rng).better_rand();
00249 }
00250 }
00251
00256 void dmatrix::rowfill_randu(const int& i, const random_number_generator& rng)
00257 {
00258 for (int j=colmin(); j<=colmax(); j++)
00259 {
00260 elem(i,j)=((random_number_generator&) rng).better_rand();
00261 }
00262 }
00263
00268 void dvector::fill_randn(const random_number_generator& rng)
00269 {
00270 for (int i=indexmin(); i<=indexmax(); i++)
00271 {
00272 (*this)(i)=randn(rng);
00273 }
00274 }
00275
00280 void dmatrix::fill_randn(const random_number_generator& rng)
00281 {
00282 for (int i=rowmin(); i<=rowmax(); i++)
00283 {
00284 elem(i).fill_randn(rng);
00285 }
00286 }
00287
00292 void d3_array::fill_randn(const random_number_generator& rng)
00293 {
00294 for (int i=slicemin(); i<=slicemax(); i++)
00295 {
00296 elem(i).fill_randn(rng);
00297 }
00298 }
00299
00304 void d3_array::fill_randu(const random_number_generator& rng)
00305 {
00306 for (int i=slicemin(); i<=slicemax(); i++)
00307 {
00308 elem(i).fill_randu(rng);
00309 }
00310 }
00311
00316 void dmatrix::fill_randu(const random_number_generator& rng)
00317 {
00318 for (int i=rowmin(); i<=rowmax(); i++)
00319 {
00320 elem(i).fill_randu(rng);
00321 }
00322 }
00323
00328 void dmatrix::colfill_randn(const int&j, const random_number_generator& rng)
00329 {
00330 for (int i=rowmin(); i<=rowmax(); i++)
00331 {
00332 elem(i,j)=randn(rng);
00333 }
00334 }
00335
00340 void dmatrix::rowfill_randn(const int& i, const random_number_generator& rng)
00341 {
00342 for (int j=colmin(); j<=colmax(); j++)
00343 {
00344 elem(i,j)=randn(rng);
00345 }
00346 }