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
00061
00062
00063
00064
00065
00066 #ifndef RANDOM_H_JUN_21_2005
00067 #define RANDOM_H_JUN_21_2005
00068
00069 #include <cstdlib>
00070 #include <iostream>
00071 #include <vector>
00072 using namespace std;
00073
00074 #define PI 3.1415926
00075 #define INTEGRALSTEP 1000
00076 #define BigValue 900000000
00077 #define BigValue2 50000
00078 #define NormalizeBase 10
00079
00080 class Random
00081 {
00082 public:
00083 Random() : initNum_(0) {}
00084 ~Random() {}
00085
00086
00087 void init(const long& initNum)
00088 {
00089 if (initNum_ < 0)
00090 {
00091 cout << "WARNING: you can only call Random::init() once. Ignoring call."
00092 << endl;
00093 return;
00094 }
00095
00096 if (initNum >= 0)
00097 {
00098 cout << "Random::init() expects a negative number but is given "
00099 << initNum << endl;
00100 exit(-1);
00101 }
00102
00103 initNum_ = initNum;
00104
00105
00106 num2_ = 123456789;
00107 a_ = 0;
00108
00109 randomA(&initNum_);
00110
00111 }
00112
00113
00114
00115 double random()
00116 {
00117 return randomA(&initNum_);
00118
00119 }
00120
00121
00122 int randomOneOf(const int& a) { return (int) (a * random()); }
00123
00124
00125 private:
00126 double randomA(long* initNum)
00127 {
00128 int c;
00129 long d;
00130 double tmp;
00131
00132 if (*initNum <= 0 || !a_)
00133 {
00134 if (-(*initNum) < 1)
00135 *initNum = 1;
00136 else
00137 *initNum = -(*initNum);
00138
00139 for (c = 32+7; c >= 0; c--)
00140 {
00141 d = (*initNum)/127773;
00142 *initNum = 16807 * (*initNum - d*127773) - 2836*d;
00143
00144 if (*initNum < 0)
00145 *initNum += 2147483647;
00146
00147 if (c < 32)
00148 b_[c] = *initNum;
00149 }
00150 a_ = b_[0];
00151 }
00152 d = (*initNum)/127773;
00153 *initNum = 16807 * (*initNum - d*127773) - 2836*d;
00154 if (*initNum < 0)
00155 *initNum += 2147483647;
00156 c = a_ / (1 + (2147483647-1)/ 32);
00157 a_ = b_[c];
00158 b_[c] = *initNum;
00159 if ((tmp = (1.0/2147483647)*a_) > (1.0 - 1.2e-7))
00160 return (1.0 - 1.2e-7);
00161 else
00162 return tmp;
00163 }
00164
00165
00166 double randomB(long* initNum)
00167 {
00168 int c;
00169 long d;
00170 double tmp;
00171
00172 if (*initNum <= 0)
00173 {
00174 if (-(*initNum) < 1)
00175 *initNum = 1;
00176 else
00177 *initNum = -(*initNum);
00178
00179 num2_ = (*initNum);
00180
00181 for (c = 32+7; c >= 0; c--)
00182 {
00183 d = (*initNum)/53668;
00184 *initNum = 40014 * (*initNum-d*53668) - d*12211;
00185
00186 if (*initNum < 0)
00187 *initNum += 2147483563;
00188
00189 if (c < 32)
00190 b_[c] = *initNum;
00191 }
00192 a_ = b_[0];
00193 }
00194
00195 d = (*initNum)/53668;
00196 *initNum = 40014 * (*initNum-d*53668) - d*12211;
00197 if (*initNum < 0) *initNum += 2147483563;
00198 d = num2_/52774;
00199 num2_ = 40692 * (num2_-d*52774) - d*3791;
00200 if (num2_ < 0)
00201 num2_ += 2147483399;
00202 c = a_ / (1 + (2147483563-1)/32);
00203 a_ = b_[c] - num2_;
00204 b_[c] = *initNum;
00205 if (a_ < 1) a_ += (2147483563-1);
00206 if ((tmp = (1.0/2147483563)*a_) > (1.0 - 1.2e-7))
00207 return (1.0 - 1.2e-7);
00208 else
00209 return tmp;
00210 }
00211
00212
00213 private:
00214 long initNum_;
00215 long num2_;
00216 long a_;
00217 long b_[32];
00218
00219 };
00220
00221 class ExtRandom
00222 {
00223 public:
00224
00225
00226 static double uniformRandom();
00227
00228
00229 static double gaussRandom(double mu, double sd);
00230
00231
00232 static double expRandom(double lambda);
00233
00234
00235 static double ComputeGauss(double mu, double sigma, double x);
00236
00237
00238 static double ComputeLnGauss(double mu, double sigma, double v1, double v2, double x);
00239
00240
00241 static double GaussianIntegral(double mu, double sigma, double v1, double v2);
00242
00243
00244 static void GaussianParaLearning(double &mu, double &sigma, double &v1, double &v2, vector<double> Tdata);
00245
00246
00247 private:
00248 static bool deviate_available;
00249 static double second_deviate;
00250 };
00251
00252
00253 #endif