Changeset 3403 in Sophya for trunk/SophyaLib/BaseTools/stsrand.cc
- Timestamp:
- Nov 23, 2007, 6:58:44 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/BaseTools/stsrand.cc
r3389 r3403 1 1 #include "stsrand.h" 2 2 #include "thsafeop.h" 3 #include "srandgen.h"3 //#include "srandgen.h" 4 4 #include "fiondblock.h" 5 5 #include <math.h> 6 #include <sys/time.h> 6 7 7 8 namespace SOPHYA { … … 57 58 58 59 void RandomGenerator::AutoInit(int lp) 59 { 60 if (ths_rand == NULL) ths_rand = new ThSafeOp; 61 ths_rand->lock(); 62 if(lp) cout << "RandomGenerator::AutoInit() : Calling Auto_Ini_Ranf() ..." << endl; 63 Auto_Ini_Ranf(lp); // Faut-il faire copier/coller du code Auto_Ini_Ranf() ici ? 64 ths_rand->unlock(); 60 // Initialisation automatique (pseudo) aleatoire du generateur. 61 // L'initialiseur est donne par un codage du nombre de millisecondes 62 // ecoulees depuis le 0 heure le 1er Janvier 1970 UTC (cf gettimeofday). 63 // Seuls les 48 bits de poids faible sont retenus. 64 // Un melange des bits est ensuite effectue pour que les 3 nombres 65 // (unsigned short) d'initialisation ne soient pas trop semblables. 66 // 67 // Le nombre le plus grand que l'on peut mettre 68 // dans un entier unsigned de N bits est: 2^N-1 69 // 48 bits -> 2^48-1 = 281474976710655 musec = 3257.8j = 8.9y 70 // -> meme initialisation tous les 8.9 ans a 1 microsec pres ! 71 { 72 // On recupere le temps ecoule depuis l'origine code en sec+musec 73 struct timeval now; 74 gettimeofday(&now,0); 75 76 // Calcul du temps ecoule depuis l'origine en microsecondes 77 uint_8 v = (uint_8)now.tv_sec*(uint_8)1000000 + (uint_8)now.tv_usec; 78 if(lp>1) cout<<"..."<<now.tv_sec<<" sec + "<<now.tv_usec<<" musec = "<<v<<" musec"<<endl; 79 80 // Remplissage du tableau de bits 81 unsigned short b[48]; 82 for(int ip=0;ip<48;ip++) {b[ip] = v&1; v = (v>>1);} 83 if(lp>2) { 84 cout<<"...b= "; 85 for(int ip=47;ip>=0;ip--) {if(ip==23) cout<<" "; cout<<b[ip];} 86 cout<<endl; 87 } 88 89 // Melange des bits qui varient vite (poids faible, microsec) 90 // avec ceux variant lentement (poids fort, sec) 91 // On coupe le mot en trois: bits[0-15], bits[16-31] et bits[32-47] 92 // On echange 2 bits sur 3 du mot bits[0-15] dans les autres mots 93 // bit0 <-> bit0 , bit1 <-> bit17 , bit2 <-> bit34 94 // bit3 <-> bit3 , bit4 <-> bit20 , bit5 <-> bit37 95 // bit13 <-> bit13 , bit14 <-> bit30 , bit15 <-> bit47 96 for(int ip=0;ip<16;ip++) { 97 if(ip%3==0) continue; 98 int ipd = (ip%3==1)? 16+ip : 32+ip; 99 unsigned short w = b[ip]; 100 //cout<<"swap g["<<ip<<"]="<<b[ip]<<" <-> b["<<ipd<<"]="<<b[ipd]<<endl; 101 b[ip] = b[ipd]; 102 b[ipd] = w; 103 } 104 if(lp>2) { 105 cout<<"...b= "; 106 for(int ip=47;ip>=0;ip--) {if(ip==23) cout<<" "; cout<<b[ip];} 107 cout<<endl; 108 } 109 110 // Construction du tableau d'initialisation 111 unsigned short seed_16v[3] = {0,0,0}; 112 for(int is=0;is<3;is++) { 113 unsigned short w = 1; 114 for(int ip=0;ip<16;ip++) { 115 seed_16v[is] += w*b[is*16+ip]; 116 w *= 2; 117 } 118 } 119 if(lp>0) cout<<"RandomGenerator::AutoInit: "<<seed_16v[0]<<" "<<seed_16v[1]<<" "<<seed_16v[2]<<endl; 120 121 // Initialise drand48() 122 Init(seed_16v,lp); 65 123 } 66 124
Note:
See TracChangeset
for help on using the changeset viewer.