Changeset 4019 in Sophya for trunk/SophyaLib/BaseTools/randinterf.cc
- Timestamp:
- Sep 24, 2011, 6:31:08 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/BaseTools/randinterf.cc
r4018 r4019 88 88 ///////////////////////////////////////////////////////////////////////// 89 89 void RandomGeneratorInterface::GenerateSeedVector(int nseed,vector<uint_2>& seed,int lp) 90 // renvoie un vecteur de nseed+2 entiers 32bits91 // [0 - 2] = codage sur 48 bits du nombre (melange) de microsec depuis l'origine90 // 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 92 92 // [3 -> 3+ngene-1] = entiers aleatoires (poor man generator) 93 93 // … … 112 112 // Calcul du temps ecoule depuis l'origine en microsecondes 113 113 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; 115 115 // Remplissage du tableau de 48 bits 116 116 uint_2 b[48]; uint_8 tdum = tmicro70; … … 125 125 for(int ip=0;ip<16;ip++) { 126 126 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]); 128 128 } 129 129 if(lp>2) { … … 139 139 for(int ip=0;ip<16;ip++) {seed[i] += w*b[i*16+ip]; w *= 2;} 140 140 } 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); 142 144 143 145 // --- … … 160 162 // the requirements. The constants below are arbitrary otherwise 161 163 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; 163 165 uint_4 state_s1, state_s2, state_s3; 164 166 state_s1 = 1243598713U ^ seed0; if (state_s1 < 2) state_s1 = 1243598713U; … … 177 179 uint_2 s = uint_2( (s1^s2^s3)&0xFFFFU ); 178 180 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; 180 182 nfill++; 181 183 } … … 356 358 r_8 RandomGeneratorInterface::GaussianPolarBoxMuller() 357 359 { 358 double x1,x2,w;359 do {360 double x1,x2,w; 361 do { 360 362 x1 = 2.0 * Next() - 1.0; 361 363 x2 = 2.0 * Next() - 1.0; 362 364 w = x1 * x1 + x2 * x2; 363 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); 365 367 } 366 368 … … 368 370 r_8 RandomGeneratorInterface::GaussianRatioUnif() 369 371 { 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; 380 382 } 381 383 382 384 r_8 RandomGeneratorInterface::GaussianLevaRatioUnif() 383 385 { 384 double u,v,x,y,q;386 double u,v,x,y,q; 385 387 do { 386 388 u = 1.-Next(); // in ]0,1] … … 525 527 * This implementation does one-sided upper-tailed deviates. 526 528 */ 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 } 561 548 } 562 549
Note:
See TracChangeset
for help on using the changeset viewer.