Ignore:
Timestamp:
Sep 24, 2011, 6:31:08 PM (14 years ago)
Author:
cmv
Message:

ajout generateurs tinymt32+64, cmv 24/09/2011

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/BaseTools/randinterf.cc

    r4018 r4019  
    8888/////////////////////////////////////////////////////////////////////////
    8989void RandomGeneratorInterface::GenerateSeedVector(int nseed,vector<uint_2>& seed,int lp)
    90 // renvoie un vecteur de nseed+2 entiers 32 bits
    91 // [0 - 2] = codage sur 48 bits du nombre (melange) de microsec depuis l'origine
     90// renvoie un vecteur de 3+nseed entiers 16 bits
     91// [0 - 2] = codage sur 3*16 = 48 bits du nombre (melange) de microsec depuis l'origine
    9292// [3 -> 3+ngene-1] = entiers aleatoires (poor man generator)
    9393//
     
    112112  // Calcul du temps ecoule depuis l'origine en microsecondes
    113113  uint_8 tmicro70 = (uint_8)now.tv_sec*(uint_8)1000000 + (uint_8)now.tv_usec;
    114   if(lp>1) cout<<"."<<now.tv_sec<<" sec + "<<now.tv_usec<<" musec = "<<tmicro70<<" musec"<<endl;
     114  if(lp>1) cout<<".since orig: "<<now.tv_sec<<" sec + "<<now.tv_usec<<" musec = "<<tmicro70<<" musec"<<endl;
    115115  // Remplissage du tableau de 48 bits
    116116  uint_2 b[48]; uint_8 tdum = tmicro70;
     
    125125  for(int ip=0;ip<16;ip++) {
    126126    if(ip%3==1) swap(b[ip],b[32+ip]);
    127     else if(ip%3==2) swap(b[ip],b[16-ip]);
     127    else if(ip%3==2) swap(b[ip],b[16+ip]);
    128128  }
    129129  if(lp>2) {
     
    139139    for(int ip=0;ip<16;ip++) {seed[i] += w*b[i*16+ip]; w *= 2;}
    140140  }
    141   if(lp>0) cout<<"seed(time): "<<seed[0]<<" "<<seed[1]<<" "<<seed[2]<<endl;
     141  if(lp>0) cout<<"seed_time[0-2]: "<<seed[0]<<" "<<seed[1]<<" "<<seed[2]<<endl;
     142  // On recree tmicro70 avec les bits melanges
     143  tmicro70 = uint_8(seed[0]) | (uint_8(seed[1])<<16) | (uint_8(seed[2])<<32);
    142144
    143145  // ---
     
    160162  // the requirements. The constants below are arbitrary otherwise
    161163  uint_4 seed0 = uint_4(tmicro70&0xFFFFFFFFULL);
    162   if(lp>2) cout<<"seed0(time): "<<seed0<<endl;
     164  if(lp>2) cout<<"seed0_time_init_t88: "<<seed0<<endl;
    163165  uint_4 state_s1, state_s2, state_s3;
    164166  state_s1 = 1243598713U ^ seed0; if (state_s1 <  2) state_s1 = 1243598713U;
     
    177179    uint_2 s = uint_2( (s1^s2^s3)&0xFFFFU );
    178180    seed.push_back(s);
    179     if(lp>0) cout<<"seed(t88): "<<seed[3+nfill]<<endl;
     181    if(lp>0) cout<<"seed_t88["<<nfill+3<<"]: "<<seed[3+nfill]<<endl;
    180182    nfill++;
    181183  }
     
    356358r_8 RandomGeneratorInterface::GaussianPolarBoxMuller()
    357359{
    358 double x1,x2,w;
    359 do {
     360 double x1,x2,w;
     361 do {
    360362   x1 = 2.0 * Next() - 1.0;
    361363   x2 = 2.0 * Next() - 1.0;
    362364   w = x1 * x1 + x2 * x2;
    363    } while ( w >= 1.0 || w==0. );
    364 return x1 * sqrt(-2.0*log(w)/w);
     365 } while ( w >= 1.0 || w==0. );
     366 return x1 * sqrt(-2.0*log(w)/w);
    365367}
    366368
     
    368370r_8 RandomGeneratorInterface::GaussianRatioUnif()
    369371{
    370 double u,v,x;
    371 while(true) {
    372   do {u = Next();} while ( u == 0. );
    373   v = (2.0*Next()-1.0)*s2se_RatioUnif;
    374   x = v/u;
    375   if(x*x <= 5.0-4.0*ep1q_RatioUnif*u) break;
    376   if(x*x<4.0*epm135_RatioUnif/u+1.4)
    377     if(v*v<-4.0*u*u*log(u)) break;
    378 }
    379 return x;
     372 double u,v,x;
     373 while(true) {
     374   do {u = Next();} while ( u == 0. );
     375   v = (2.0*Next()-1.0)*s2se_RatioUnif;
     376   x = v/u;
     377   if(x*x <= 5.0-4.0*ep1q_RatioUnif*u) break;
     378   if(x*x<4.0*epm135_RatioUnif/u+1.4)
     379     if(v*v<-4.0*u*u*log(u)) break;
     380 }
     381 return x;
    380382}
    381383
    382384r_8 RandomGeneratorInterface::GaussianLevaRatioUnif()
    383385{
    384 double u,v,x,y,q;
     386 double u,v,x,y,q;
    385387 do {
    386388   u = 1.-Next();  // in ]0,1]
     
    525527   * This implementation does one-sided upper-tailed deviates.
    526528   */
    527 
    528   if (s < slim)
    529     {
    530       /* For small s, use a direct rejection method. The limit s < 1
    531          can be adjusted to optimise the overall efficiency */
    532       double x;
    533       do
    534         {
    535           x = Gaussian();
    536         }
    537       while (x < s);
    538       return x;
    539     }
    540   else
    541     {
    542       /* Use the "supertail" deviates from the last two steps
    543        * of Marsaglia's rectangle-wedge-tail method, as described
    544        * in Knuth, v2, 3rd ed, pp 123-128.  (See also exercise 11, p139,
    545        * and the solution, p586.)
    546        */
    547       double u, v, x;
    548       do
    549         {
    550           u = Next();
    551           do
    552             {
    553               v = Next();
    554             }
    555           while (v == 0.0);
    556           x = sqrt (s * s - 2 * log (v));
    557         }
    558       while (x * u > s);
    559       return x;
    560     }
     529  if(s < slim) {
     530    /* For small s, use a direct rejection method. The limit s < 1
     531       can be adjusted to optimise the overall efficiency */
     532    double x;
     533    do {x = Gaussian();} while (x < s);
     534    return x;
     535  } else {
     536    /* Use the "supertail" deviates from the last two steps
     537     * of Marsaglia's rectangle-wedge-tail method, as described
     538     * in Knuth, v2, 3rd ed, pp 123-128.  (See also exercise 11, p139,
     539     * and the solution, p586.)
     540     */
     541    double u, v, x;
     542    do {u = Next();
     543      do {v = Next();} while (v == 0.0);
     544      x = sqrt (s * s - 2 * log (v));
     545    } while (x * u > s);
     546    return x;
     547  }
    561548}
    562549
Note: See TracChangeset for help on using the changeset viewer.