Main Page | Namespace List | Class Hierarchy | Compound List | File List | Compound Members | File Members

Random.cpp

Go to the documentation of this file.
00001 
00012 #include <Random.h>
00013 
00014 using namespace std;
00015 
00016 #define NRAND_SAMPLES 5
00017 #define Uniform randreal2
00018 
00019 #define Gmaximo 5000
00020 
00026 Random::Random() {
00027   Seed   = 0 ;
00028   isinit = 0 ;
00029 } //Constructor
00030 
00036 Random::~Random() {
00037 } //Destructor
00038 
00043 int Random::flip(float prob)
00044 {
00045   //float randomperc();
00046     if(randomperc() <= prob)
00047   return(1);
00048     else
00049   return(0);
00050 }
00051 
00052 
00056 void Random::advance_random()
00057 {
00058   int j1;
00059   double new_random;
00060 
00061   for(j1 = 0; j1 < 24; j1++)
00062   {
00063   new_random = oldrand[j1] - oldrand[j1+31];
00064   if(new_random < 0.0) new_random = new_random + 1.0;
00065   oldrand[j1] = new_random;
00066   }
00067 
00068   for(j1 = 24; j1 < 55; j1++)
00069   {
00070   new_random = oldrand [j1] - oldrand [j1-24];
00071   if(new_random < 0.0) new_random = new_random + 1.0;
00072   oldrand[j1] = new_random;
00073   }
00074 }
00075 
00076 
00080 void Random::initrandomnormaldeviate()
00081 {
00082     rndcalcflag = 1;
00083 }
00084 
00090 double Random::noise(double mu,double sigma)
00091 {
00092     //double randomnormaldeviate();
00093     return((randomnormaldeviate()*sigma) + mu);
00094 }
00095 
00099 void Random::randomize()
00100 
00101 {
00102   int j1;
00103 
00104     for(j1=0; j1<=54; j1++)
00105       oldrand[j1] = 0.0;
00106 
00107   jrand=0;
00108 
00109    warmup_random(Rseed);
00110 }
00111 
00115 double Random::randomnormaldeviate()
00116 
00117 {
00118 //    double sqrt(), log(), sin(), cos();
00119     //float randomperc();
00120   double t, rndx1;
00121 
00122     if(rndcalcflag)
00123     {
00124   rndx1 = sqrt(- 2.0*log((double) randomperc()));
00125   t = 6.2831853072 * (double) randomperc();
00126   rndx2 = sin(t);
00127   rndcalcflag = 0;
00128   return(rndx1 * cos(t));
00129     }
00130 
00131     else
00132 
00133     {
00134   rndcalcflag = 1;
00135   return(rndx2);
00136   }
00137 
00138 }
00139 
00147 float Random::randomperc()
00148 {
00149 //cout << "randomperc. 0; " << endl; 
00150     jrand++;
00151 //cout << "randomperc. 1" << endl;
00152 
00153     if(jrand >= 55)
00154   {
00155   jrand = 1;
00156   advance_random();
00157     }
00158 
00159     return((float) oldrand[jrand]); 
00160 //    return 0.0;
00161 }
00162 
00166 int Random::rnd(int low, int high)
00167 
00168 {
00169     int i;
00170   //float randomperc();
00171 //cout << "Generating a random number..." << endl;
00172     if(low >= high)
00173        i = low;
00174     else
00175     {
00176 //cout << "Generating a random number 1 ..." << endl;
00177        i =(int) (randomperc() * (high - low + 1)) + low;
00178 //cout << "Generating a random number 2 ..." << endl;
00179       if(i > high) 
00180         i = high;
00181     }
00182 
00183   return(i);
00184 }
00185 
00189 float Random::rndreal(float lo ,float hi)
00190 
00191 {
00192   return((randomperc() * (hi - lo)) + lo);
00193 }
00194 
00198 void Random::warmup_random(float random_seed)
00199 
00200 {
00201   int j1, ii;
00202   double new_random, prev_random;
00203 
00204   oldrand[54] = random_seed;
00205   new_random = 0.000000001;
00206   prev_random = random_seed;
00207 
00208   for(j1 = 1 ; j1 <= 54; j1++)
00209 
00210   {
00211   ii = (21*j1)%54;
00212   oldrand[ii] = new_random;
00213   new_random = prev_random-new_random;
00214   if(new_random<0.0) new_random = new_random + 1.0;
00215   prev_random = oldrand[ii];
00216   }
00217 
00218   advance_random();
00219   advance_random();
00220   advance_random();
00221 
00222   jrand = 0;
00223 }
00224 
00225 /********************* RandEvolutiva ******************************************/
00229 double Random::randreal2(void)
00230 {
00231   double result;
00232 
00233   if (!isinit) {
00234     srand(Seed);
00235     isinit = 1;
00236   }
00237   result = ((double) rand());
00238   result /= RAND_MAX;
00239 
00240   return (result);
00241 }
00242 
00248 double Random::Gauss(double sigma)
00249 {
00250   /* System generated locals */
00251   double ret_val;
00252 
00253   /* Local variables */
00254   static double u, x, y, u0, u1, u2;
00255 
00256 
00257   /*  SIGMA --> standard deviation */
00258 
00259   /* L1: */
00260   u = Uniform();
00261   u0 = Uniform();
00262   if (u >= .919544406) {
00263     goto L2;
00264   }
00265   x = (u0 + u * .825339283) * 2.40375766 - 2.11402808;
00266     goto L10;
00267   L2:
00268     if (u < .965487131) {
00269       goto L4;
00270     }
00271   L3:
00272     u1 = Uniform();
00273     y = sqrt(4.46911474 - log(u1) * 2.);
00274     u2 = Uniform();
00275     if (y * u2 > 2.11402808) {
00276       goto L3;
00277     }
00278     goto L9;
00279   L4:
00280     if (u < .949990709) {
00281       goto L6;
00282     }
00283   L5:
00284     u1 = Uniform();
00285     y = u1 * .273629336 + 1.84039875;
00286     u2 = Uniform();
00287     if (exp(y * -.5 * y) * .39894228 - .443299126 + y * .209694057 < u2 *
00288       .0427025816) {
00289       goto L5;
00290     }
00291     goto L9;
00292   L6:
00293     if (u < .925852334) {
00294       goto L8;
00295     }
00296   L7:
00297     u1 = Uniform();
00298     y = u1 * 1.55066917 + .289729574;
00299     u2 = Uniform();
00300     if (exp(y * -.5 * y) * .39894228 - .443299126 + y * .209694057 < u2 *
00301       .0159745227) {
00302       goto L7;
00303     }
00304     goto L9;
00305   L8:
00306     u1 = Uniform();
00307     y = u1 * .289729574;
00308     u2 = Uniform();
00309     if (exp(y * -.5 * y) * .39894228 - .382544556 < u2 * .0163977244) {
00310       goto L8;
00311     }
00312   L9:
00313     x = y;
00314     if (u0 >= .5) {
00315       x = -y;
00316     }
00317   L10:
00318     ret_val = sigma * x;
00319     return ret_val;
00320 } /* Gauss() */
00321 
00325 double Random::N(double m, double sigma)
00326 {
00327   return m + Gauss(sigma);
00328 }
00329 
00334 void Random::initrandom(int seed)
00335 {
00336   Seed = seed;
00337 }
00338 
00339 
00340 int Random::randint(int lo, int hi)
00341 {
00342   return (lo + (int) randreal2() * (hi - lo + 1));
00343 }
00344 

Generated on Wed Feb 11 10:38:02 2004 for Paes by doxygen 1.3.3