[851] | 1 | #include "machdefs.h"
|
---|
| 2 | #include <stdlib.h>
|
---|
| 3 | #include <stdio.h>
|
---|
| 4 | #include <string.h>
|
---|
| 5 | #include <math.h>
|
---|
| 6 | #include <sys/time.h>
|
---|
| 7 | #include "srandgen.h"
|
---|
| 8 | #include "smathconst.h"
|
---|
| 9 |
|
---|
| 10 | static double GAU_RANGE=6.;
|
---|
| 11 |
|
---|
| 12 | /*
|
---|
| 13 | ++
|
---|
| 14 | Module Tirages aleatoires (C)
|
---|
| 15 | Lib LibsUtil
|
---|
[3074] | 16 | include srandgen.h
|
---|
[851] | 17 | --
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | /*
|
---|
| 21 | ++
|
---|
| 22 | frand01()
|
---|
| 23 | tirage aleatoire entre 0 et 1, retourne float
|
---|
| 24 | drand01()
|
---|
| 25 | tirage aleatoire entre 0 et 1, retourne double
|
---|
| 26 | rand01()
|
---|
| 27 | c'est le defaut: drand01()
|
---|
| 28 | frandpm1()
|
---|
| 29 | tirage aleatoire entre -1 et 1, retourne float
|
---|
| 30 | drandpm1()
|
---|
| 31 | tirage aleatoire entre -1 et 1, retourne double
|
---|
| 32 | ranfpm1()
|
---|
| 33 | c'est le defaut: drandpm1()
|
---|
| 34 | --
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[1607] | 37 | /*! \ingroup BaseTools
|
---|
[913] | 38 | \fn frand01
|
---|
| 39 | Returns a random number (float) with flat distribution between 0 ... 1
|
---|
| 40 | */
|
---|
[1607] | 41 | /*! \ingroup BaseTools
|
---|
[913] | 42 | \fn drand01
|
---|
| 43 | Returns a random number (double) with flat distribution between 0 ... 1
|
---|
| 44 | */
|
---|
[1607] | 45 | /*! \ingroup BaseTools
|
---|
[913] | 46 | \fn frandpm1()
|
---|
| 47 | Returns a random number (float) with flat distribution between -1 ... 1
|
---|
| 48 | */
|
---|
[1607] | 49 | /*! \ingroup BaseTools
|
---|
[913] | 50 | \fn drandpm1()
|
---|
| 51 | Returns a random number (double) with flat distribution between -1 ... 1
|
---|
| 52 | */
|
---|
| 53 |
|
---|
[851] | 54 | /*=========================================================================*/
|
---|
| 55 | /*
|
---|
| 56 | ++
|
---|
| 57 | void Ini_Ranf_Quick(long seed_val, int lp)
|
---|
| 58 | Initialisation rapide du generateur (drand48) par un entier
|
---|
| 59 | de 32 bits de type long (cf srand48).
|
---|
| 60 | --
|
---|
| 61 | */
|
---|
[913] | 62 |
|
---|
[1607] | 63 | /*! \ingroup BaseTools
|
---|
[913] | 64 | Fast initialisation of the random number generator \c (drand48)
|
---|
| 65 | using a \c long type value (cf \c srand48 )
|
---|
| 66 | */
|
---|
[851] | 67 | void Ini_Ranf_Quick(long seed_val, int lp)
|
---|
| 68 | {
|
---|
| 69 | if(lp) printf("Ini_Ranf_Quick: %d\n",(int) seed_val);
|
---|
| 70 | srand48(seed_val);
|
---|
| 71 | return;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /*=========================================================================*/
|
---|
| 75 | /*
|
---|
| 76 | ++
|
---|
| 77 | void Ini_Ranf(unsigned short seed_16v[3], int lp)
|
---|
| 78 | Initialisation complete du generateur (drand48) par
|
---|
| 79 | 48 bits (cf seed48).
|
---|
| 80 | --
|
---|
| 81 | */
|
---|
[1607] | 82 | /*! \ingroup BaseTools
|
---|
[913] | 83 | complete initialisation of the random number generator \c (drand48)
|
---|
| 84 | using a \c using 48 bits (cf \c seed48 )
|
---|
| 85 | */
|
---|
[851] | 86 | void Ini_Ranf(unsigned short seed_16v[3], int lp)
|
---|
| 87 | {
|
---|
| 88 | if(lp) printf("Ini_Ranf: %d %d %d\n"
|
---|
| 89 | ,seed_16v[0],seed_16v[1],seed_16v[2]);
|
---|
| 90 | seed48(seed_16v);
|
---|
| 91 | return;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | /*=========================================================================*/
|
---|
| 95 | /*
|
---|
| 96 | ++
|
---|
| 97 | void Get_Ranf(unsigned short seed_16v[3], int lp)
|
---|
| 98 | Recuperation de l'etat du generateur (drand48) sur
|
---|
| 99 | de 48 bits (cf seed48).
|
---|
| 100 | --
|
---|
| 101 | */
|
---|
[1607] | 102 | /*! \ingroup BaseTools
|
---|
[913] | 103 | Returns the status (48 bits) of the random number generator \c (drand48)
|
---|
| 104 | (cf \c seed48 )
|
---|
| 105 | */
|
---|
[851] | 106 | void Get_Ranf(unsigned short seed_16v[3], int lp)
|
---|
| 107 | {
|
---|
| 108 | unsigned short seed[3] = {0,0,0};
|
---|
| 109 | unsigned short *p;
|
---|
| 110 | p = seed48(seed);
|
---|
| 111 | memcpy(seed_16v,p,3*sizeof(unsigned short));
|
---|
| 112 | if(lp) printf("Get_Ranf: %d %d %d\n"
|
---|
| 113 | ,seed_16v[0],seed_16v[1],seed_16v[2]);
|
---|
| 114 | /* on re-initialise a ce qui etait avant */
|
---|
| 115 | seed48(seed_16v);
|
---|
| 116 | return;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | /*=========================================================================*/
|
---|
| 120 | /*
|
---|
| 121 | ++
|
---|
| 122 | void Auto_Ini_Ranf(int lp)
|
---|
| 123 | Initialisation automatique (pseudo) aleatoire du generateur.
|
---|
| 124 | L'initialiseur est donne par le nombre de millisecondes
|
---|
| 125 | ecoulees depuis le dernier jour multiple de 23 du nombre de jours
|
---|
| 126 | depuis le 0 heure le 1er Janvier 1970 UTC (cf gettimeofday).
|
---|
| 127 | Pour retomber sur la meme initialisation
|
---|
| 128 | il faut generer deux aleatoires a moins de 1/1000 seconde
|
---|
| 129 | ou generer le deuxieme aleatoire 23 jours apres le premier
|
---|
| 130 | a la meme heure (a 1/1000 de seconde pres). La fonction
|
---|
| 131 | d'initialisation utilisee est Ini_Ranf_Quick(long).
|
---|
| 132 | --
|
---|
| 133 | */
|
---|
[1607] | 134 | /*! \ingroup BaseTools
|
---|
[913] | 135 | Automatic initialisation using the present time
|
---|
| 136 | */
|
---|
[851] | 137 | void Auto_Ini_Ranf(int lp)
|
---|
| 138 | {
|
---|
| 139 | struct timeval now;
|
---|
| 140 | long nj,nj23,seed=0;
|
---|
| 141 | double buf;
|
---|
| 142 |
|
---|
| 143 | gettimeofday (&now,0);
|
---|
| 144 |
|
---|
| 145 | /* dans 32 bits signes on met environ 23 jours a 1/1000 de seconde pres! */
|
---|
| 146 | /* Nombre de jours depuis l'origine */
|
---|
| 147 | nj = (long) now.tv_sec / 86400;
|
---|
| 148 | /* Nombre de jours depuis le dernier jour multiple de 23 jours */
|
---|
| 149 | nj23 = nj % 23;
|
---|
| 150 | /* nombre de secondes depuis le dernier jour multiple de 23 jours */
|
---|
| 151 | buf = (double) (nj23*86400 + (now.tv_sec-nj*86400));
|
---|
| 152 | /* nombre de milliemes de secondes depuis ... */
|
---|
| 153 | buf = buf*1000. + now.tv_usec/1000.;
|
---|
| 154 | seed = (long) buf;
|
---|
| 155 |
|
---|
| 156 | if(lp) {
|
---|
[1586] | 157 | printf("Auto_Ini_Ranf: date %ld s %ld 10^-6 sec seed=%ld:\n"
|
---|
| 158 | ,(long)now.tv_sec,(long)now.tv_usec,seed);
|
---|
[1585] | 159 | if(lp>1) printf("... njours=%ld nj23=%ld buf=%.20g\n",nj,nj23,buf);
|
---|
[851] | 160 | }
|
---|
| 161 | Ini_Ranf_Quick(seed,lp);
|
---|
| 162 | return;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | /*=========================================================================*/
|
---|
| 166 | /*
|
---|
| 167 | ++
|
---|
| 168 | void SetGauRange(double range)
|
---|
| 169 | Generation de distribution gaussienne:
|
---|
| 170 | Changement de l'initialisation de l'excursion du tirage
|
---|
[3074] | 171 | en nombre de sigmas (obsolete).
|
---|
[851] | 172 | --
|
---|
| 173 | */
|
---|
| 174 | void SetGauRange(double range)
|
---|
| 175 | {
|
---|
| 176 | GAU_RANGE = range;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | /*=========================================================================*/
|
---|
| 180 | /*
|
---|
| 181 | ++
|
---|
| 182 | float NorRand(void)
|
---|
| 183 | Generation aleatoire gaussienne normee centree
|
---|
| 184 | --
|
---|
| 185 | */
|
---|
[1607] | 186 | /*! \ingroup BaseTools
|
---|
[913] | 187 | Normal (Gaussian) random number generator (Mean=0., Sigma=1.)
|
---|
| 188 | */
|
---|
[851] | 189 | float NorRand(void)
|
---|
| 190 | {
|
---|
| 191 | double x,A,B;
|
---|
| 192 |
|
---|
| 193 | LAB10:
|
---|
| 194 | A = drand01();
|
---|
| 195 | if ( A == 0. ) goto LAB10;
|
---|
| 196 | B = drand01();
|
---|
| 197 | x = sqrt(-2.*log(A))*cos(DeuxPi*B);
|
---|
| 198 | return( (float) x );
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | /*=========================================================================*/
|
---|
| 202 | /*
|
---|
| 203 | ++
|
---|
| 204 | float NorRand1(void)
|
---|
| 205 | Generation aleatoire gaussienne normee centree
|
---|
| 206 | la distribution est limitee entre +/- GAU_RANGE (obsolete).
|
---|
| 207 | --
|
---|
| 208 | */
|
---|
| 209 | float NorRand1(void)
|
---|
| 210 | {
|
---|
| 211 | double b, x, y, gauss;
|
---|
| 212 |
|
---|
| 213 | b = 1./sqrt(2.*M_PI);
|
---|
| 214 | LAB10:
|
---|
| 215 | x = GAU_RANGE*drandpm1();
|
---|
| 216 | y = drand01()*b;
|
---|
| 217 | gauss = b*exp(-x*x/2.);
|
---|
| 218 | if ( gauss-y < 0. ) goto LAB10 ;
|
---|
| 219 | return( (float) x );
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | /*=========================================================================*/
|
---|
| 223 | /*
|
---|
| 224 | ++
|
---|
| 225 | double GauRnd(double am, double s)
|
---|
| 226 | Generation aleatoire gaussienne de centre "am" et de sigma "s".
|
---|
| 227 | --
|
---|
| 228 | */
|
---|
[1607] | 229 | /*! \ingroup BaseTools
|
---|
[913] | 230 | Normal (Gaussian) random number generator with the specified mean
|
---|
| 231 | (\c am ) and sigma (\c s )
|
---|
| 232 | */
|
---|
[851] | 233 | double GauRnd(double am, double s)
|
---|
| 234 | {
|
---|
| 235 | double x,A,B;
|
---|
| 236 |
|
---|
| 237 | LAB10:
|
---|
| 238 | A = drand01();
|
---|
| 239 | if ( A == 0. ) goto LAB10;
|
---|
| 240 | B = drand01();
|
---|
| 241 | x = am + s * sqrt(-2.*log(A))*cos(DeuxPi*B);
|
---|
| 242 | return(x);
|
---|
| 243 | }
|
---|
| 244 |
|
---|
[3099] | 245 | /*! \ingroup BaseTools
|
---|
| 246 | \brief Poisson random number generator.
|
---|
| 247 |
|
---|
| 248 | Return an integer value (>=0) corresponding a Poisson distribution with mean \b mu.
|
---|
| 249 | \warning NOT the most efficient way of generating a large series of numbers
|
---|
| 250 | with the SAME mean
|
---|
| 251 | */
|
---|
| 252 | int PoissRand(double mu)
|
---|
| 253 | {
|
---|
[3102] | 254 | double pp,ppi,x;
|
---|
| 255 | int n;
|
---|
[3099] | 256 | ppi = pp = exp(-mu);
|
---|
[3102] | 257 | x = drand01();
|
---|
| 258 | n = 0;
|
---|
[3099] | 259 | while (x > ppi) {
|
---|
| 260 | n++;
|
---|
| 261 | pp = mu*pp/(double)n;
|
---|
| 262 | ppi += pp;
|
---|
| 263 | }
|
---|
| 264 | return n;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
[851] | 267 | /*=========================================================================*/
|
---|
| 268 | /*
|
---|
| 269 | ++
|
---|
| 270 | double GauRnd1(double am, double s)
|
---|
| 271 | Generation aleatoire gaussienne de centre "am" et de
|
---|
| 272 | sigma "s" la distribution est limitee entre am +/- GAU_RANGE (obsolete).
|
---|
| 273 | --
|
---|
| 274 | */
|
---|
[3099] | 275 | /*! \ingroup BaseTools
|
---|
| 276 | \brief OBSOLETE (gaussian random number generator)
|
---|
| 277 | */
|
---|
[851] | 278 | double GauRnd1(double am, double s)
|
---|
| 279 | {
|
---|
| 280 | double s2, b, x, y, gauss;
|
---|
| 281 |
|
---|
| 282 | s2 = 2.*s*s;
|
---|
| 283 | b = 1./sqrt(2.*M_PI*s);
|
---|
| 284 | LAB10:
|
---|
| 285 | x = am + GAU_RANGE*s*drandpm1();
|
---|
| 286 | y = drand01()*b;
|
---|
| 287 | gauss = b*exp(-(x-am)*(x-am)/s2);
|
---|
| 288 | if ( gauss-y < 0. ) goto LAB10 ;
|
---|
| 289 | return(x);
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | /*==========================================================================*/
|
---|
| 293 | /*
|
---|
| 294 | ++
|
---|
| 295 | void NormGau(double *x,double *y,double mx,double my,double sa,double sb,double teta);
|
---|
| 296 | Tirage de 2 nombres aleatoires x et y distribues sur une gaussienne 2D
|
---|
| 297 | de centre (x=mx,y=my), de sigmas grand axe et petit axe (sa,sb)
|
---|
| 298 | et dont le grand axe fait un angle teta (radian) avec l'axe des x.
|
---|
| 299 | --
|
---|
| 300 | */
|
---|
| 301 | /*
|
---|
| 302 | ++
|
---|
| 303 | | - La densite de probabilite (normalisee a 1) sur laquelle on tire est:
|
---|
| 304 | | N*exp[-0.5*{ (A/sa)**2+(C/sc)**2 }], N=1/(2Pi*sa*sc)
|
---|
| 305 | | ou A et B sont les coordonnees selon le grand axe et le petit axe
|
---|
| 306 | | et teta = angle(x,A), le resultat subit ensuite une rotation d'angle teta.
|
---|
| 307 | | - La matrice des covariances C des variables A,B est:
|
---|
| 308 | | | sa^2 0 |
|
---|
| 309 | | | | et det(C) = (1-ro^2)*sa^2*sb^2
|
---|
| 310 | | | 0 sb^2 |
|
---|
| 311 | | - La distribution x,y resultante est:
|
---|
| 312 | | N*exp[-0.5*{[(dx/sx)^2-2*ro/(sx*sy)*dx*dy+(dy/sy)^2]/(1-ro^2)}]
|
---|
| 313 | | ou N est donne dans NormCo et sx,sy,ro sont calcules a partir
|
---|
| 314 | | de sa,sc,teta (voir fonctions paramga ou gaparam). La matrice des
|
---|
| 315 | | covariances des variables x,y est donnee dans la fonction NormCo.
|
---|
| 316 | --
|
---|
| 317 | */
|
---|
| 318 | void NormGau(double *x,double *y
|
---|
| 319 | ,double mx,double my,double sa,double sb,double teta)
|
---|
| 320 | {
|
---|
| 321 | double c,s,X,Y;
|
---|
| 322 |
|
---|
| 323 | LAB10:
|
---|
| 324 | s = drand01();
|
---|
| 325 | if ( s == 0. ) goto LAB10;
|
---|
| 326 | s = sqrt(-2.*log(s));
|
---|
| 327 | c = DeuxPi * drand01();
|
---|
| 328 |
|
---|
| 329 | X = sa*s*cos(c);
|
---|
| 330 | Y = sb*s*sin(c);
|
---|
| 331 |
|
---|
| 332 | c = cos(teta); s = sin(teta);
|
---|
| 333 | *x = mx + c*X - s*Y;
|
---|
| 334 | *y = my + s*X + c*Y;
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | /*==========================================================================*/
|
---|
| 338 | /*
|
---|
| 339 | ++
|
---|
| 340 | int NormCo(double *x,double *y,double mx,double my,double sx,double sy,double ro)
|
---|
| 341 | Tirage de 2 nombres aleatoires x et y distribues sur une gaussienne 2D
|
---|
| 342 | de centre (mx,my), de coefficient de correlation rho (ro) et telle que
|
---|
| 343 | les sigmas finals des variables x et y soient sx,sy (ce sont
|
---|
| 344 | les valeurs des distributions marginales des variables aleatoires x et y
|
---|
| 345 | c'est a dire les sigmas des projections x et y de l'histogramme 2D
|
---|
| 346 | de la gaussienne). Retourne 0 si ok.
|
---|
| 347 | --
|
---|
| 348 | */
|
---|
| 349 | /*
|
---|
| 350 | ++
|
---|
| 351 | | - La densite de probabilite (normalisee a 1) sur laquelle on tire est:
|
---|
| 352 | | N*exp[-0.5*{[(dx/sx)^2-2*ro/(sx*sy)*dx*dy+(dy/sy)^2]/(1-ro^2)}]
|
---|
| 353 | | avec dx = x-mx, dy = y-my et N = 1/[2Pi*sx*sy*sqrt(1-ro^2)]
|
---|
| 354 | | - Dans ce cas la distribution marginale est (ex en X):
|
---|
| 355 | | 1/(sqrt(2Pi)*sx) * exp[-0.5*{dx^2/sx^2}]
|
---|
| 356 | | - La matrice des covariances C des variables x,y est:
|
---|
| 357 | | | sx^2 ro*sx*sy |
|
---|
| 358 | | | | et det(C) = (1-ro^2)*sx^2*sy^2
|
---|
| 359 | | | ro*sx*sy sy^2 |
|
---|
| 360 | | - La matrice inverse C^(-1) est:
|
---|
| 361 | | | 1/sx^2 -ro/(sx*sy) |
|
---|
| 362 | | | | * 1/(1-ro^2)
|
---|
| 363 | | | -ro/(sx*sy) 1/sy^2 |
|
---|
| 364 | --
|
---|
| 365 | */
|
---|
| 366 | /*
|
---|
| 367 | ++
|
---|
| 368 | | - Remarque:
|
---|
| 369 | | le sigma que l'on obtient quand on fait une coupe de la gaussienne 2D
|
---|
| 370 | | en y=0 (ou x=0) est: SX0(y=0) = sx*sqrt(1-ro^2) different de sx
|
---|
| 371 | | SY0(x=0) = sy*sqrt(1-ro^2) different de sy
|
---|
| 372 | | La distribution qui correspond a des sigmas SX0,SY0
|
---|
| 373 | | pour les coupes en y=0,x=0 de la gaussienne 2D serait:
|
---|
| 374 | | N*exp[-0.5*{ (dx/SX0)^2-2*ro/(SX0*SY0)*dx*dy+(dy/SY0)^2 }]
|
---|
| 375 | | avec N = sqrt(1-ro^2)/(2Pi*SX0*SY0) et les variances
|
---|
| 376 | | des variables x,y sont toujours
|
---|
| 377 | | sx=SX0/sqrt(1-ro^2), sy=SY0/sqrt(1-ro^2)
|
---|
| 378 | --
|
---|
| 379 | */
|
---|
| 380 | int NormCo(double *x,double *y
|
---|
| 381 | ,double mx,double my,double sx,double sy,double ro)
|
---|
| 382 | {
|
---|
| 383 | double a,b,sa;
|
---|
| 384 | if( ro <= -1. || ro >= 1. ) return(1);
|
---|
| 385 | LAB10:
|
---|
| 386 | b = drand01();
|
---|
| 387 | if ( b == 0. ) goto LAB10;
|
---|
| 388 | b = sqrt(-2.*log(b));
|
---|
| 389 | a = DeuxPi * drand01();
|
---|
| 390 | sa = sin(a);
|
---|
| 391 |
|
---|
| 392 | *x = mx + sx*b*(sqrt(1.-ro*ro)*cos(a)+ro*sa);
|
---|
| 393 | *y = my + sy*b*sa;
|
---|
| 394 |
|
---|
| 395 | return(0);
|
---|
| 396 | }
|
---|
| 397 |
|
---|
[3099] | 398 |
|
---|
| 399 | /*!
|
---|
| 400 | \ingroup BaseTools
|
---|
| 401 | \class SOPHYA::RandomGenerator
|
---|
| 402 | \brief Random number generator
|
---|
| 403 |
|
---|
| 404 | This is a class with static methods, providing an alternative interface
|
---|
| 405 | to random number generations functions declared in srandgen.h.
|
---|
| 406 |
|
---|
| 407 | \sa frand01 drand01 frandpm1 drandpm1
|
---|
| 408 | \sa GauRnd PoissRand
|
---|
| 409 | \sa Ini_Ranf_Quick Ini_Ranf Get_Ranf Auto_Ini_Ranf
|
---|
| 410 | */
|
---|
| 411 |
|
---|
[851] | 412 | /*==========================================================================*/
|
---|
| 413 | /*
|
---|
| 414 | ++
|
---|
| 415 | Titre Exemple d'utilisation des aleatoires avec initialisation.
|
---|
| 416 | --
|
---|
| 417 | */
|
---|
| 418 | /*
|
---|
| 419 | ++
|
---|
[3074] | 420 | | #include "srandgen.h"
|
---|
[851] | 421 | |
|
---|
| 422 | | void main() {
|
---|
| 423 | | long i,ini=123456789;
|
---|
| 424 | | unsigned short seed[3];
|
---|
| 425 | |
|
---|
| 426 | | printf(" 1./ ==> test nitialisation par un long\n");
|
---|
| 427 | | Ini_Ranf_Quick(ini,1);
|
---|
| 428 | | for(i=0;i<10;i++) printf("%d -> %f\n",i,ranf01());
|
---|
| 429 | --
|
---|
| 430 | */
|
---|
| 431 | /*
|
---|
| 432 | ++
|
---|
| 433 | |
|
---|
| 434 | | printf("\n 2./ ==> test initialisation par tableau de 3 unsigned short\n");
|
---|
| 435 | | Ini_Ranf_Quick(ini,1);
|
---|
| 436 | | for(i=0;i<5;i++) printf("%d -> %f\n",i,ranf01());
|
---|
| 437 | | Get_Ranf(seed,1);
|
---|
| 438 | | for(i=5;i<10;i++) printf("%d -> %f\n",i,ranf01());
|
---|
| 439 | | Ini_Ranf(seed,1);
|
---|
| 440 | | for(i=5;i<10;i++) printf("%d -> %f\n",i,ranf01());
|
---|
| 441 | | Get_Ranf(seed,1);
|
---|
| 442 | --
|
---|
| 443 | */
|
---|
| 444 | /*
|
---|
| 445 | ++
|
---|
| 446 | |
|
---|
| 447 | | printf("\n 3./ ==> test initialisation automatique\n");
|
---|
| 448 | | Auto_Ini_Ranf(2);
|
---|
| 449 | | for(i=0;i<5;i++) printf("%d -> %f\n",i,ranf01());
|
---|
| 450 | | i=0; while(i<10000000) i++;
|
---|
| 451 | | Auto_Ini_Ranf(2);
|
---|
| 452 | | for(i=0;i<5;i++) printf("%d -> %f\n",i,ranf01());
|
---|
| 453 | | i=0; while(i<10000000) i++;
|
---|
| 454 | | Auto_Ini_Ranf(2);
|
---|
| 455 | | for(i=0;i<5;i++) printf("%d -> %f\n",i,ranf01());
|
---|
| 456 | | }
|
---|
| 457 | --
|
---|
| 458 | */
|
---|
| 459 | /*
|
---|
| 460 | ++
|
---|
| 461 | | 1./ ==> test initialisation par un long
|
---|
| 462 | | Ini_Ranf_Quick: 123456789
|
---|
| 463 | | 0 -> 0.052468
|
---|
| 464 | | 1 -> 0.025444
|
---|
| 465 | | 2 -> 0.099272
|
---|
| 466 | | 3 -> 0.436130
|
---|
| 467 | | 4 -> 0.327740
|
---|
| 468 | | 5 -> 0.821202
|
---|
| 469 | | 6 -> 0.560493
|
---|
| 470 | | 7 -> 0.018157
|
---|
| 471 | | 8 -> 0.872758
|
---|
| 472 | | 9 -> 0.652496
|
---|
| 473 | --
|
---|
| 474 | */
|
---|
| 475 | /*
|
---|
| 476 | ++
|
---|
| 477 | |
|
---|
| 478 | | 2./ ==> test initialisation par tableau de 3 unsigned short
|
---|
| 479 | | Ini_Ranf_Quick: 123456789
|
---|
| 480 | | 0 -> 0.052468
|
---|
| 481 | | 1 -> 0.025444
|
---|
| 482 | | 2 -> 0.099272
|
---|
| 483 | | 3 -> 0.436130
|
---|
| 484 | | 4 -> 0.327740
|
---|
| 485 | --
|
---|
| 486 | */
|
---|
| 487 | /*
|
---|
| 488 | ++
|
---|
| 489 | | Get_Ranf: 36117 51106 21478
|
---|
| 490 | | 5 -> 0.821202
|
---|
| 491 | | 6 -> 0.560493
|
---|
| 492 | | 7 -> 0.018157
|
---|
| 493 | | 8 -> 0.872758
|
---|
| 494 | | 9 -> 0.652496
|
---|
| 495 | --
|
---|
| 496 | */
|
---|
| 497 | /*
|
---|
| 498 | ++
|
---|
| 499 | | Ini_Ranf: 36117 51106 21478
|
---|
| 500 | | 5 -> 0.821202
|
---|
| 501 | | 6 -> 0.560493
|
---|
| 502 | | 7 -> 0.018157
|
---|
| 503 | | 8 -> 0.872758
|
---|
| 504 | | 9 -> 0.652496
|
---|
| 505 | | Get_Ranf: 16576 62373 42761
|
---|
| 506 | --
|
---|
| 507 | */
|
---|
| 508 | /*
|
---|
| 509 | ++
|
---|
| 510 | |
|
---|
| 511 | | 3./ ==> test initialisation automatique
|
---|
| 512 | | Auto_Ini_Ranf: date 887117206 s 868138 10^-6 sec seed=826006868:
|
---|
| 513 | | ... njours=10267 nj23=9 buf=826006868.13800001
|
---|
| 514 | | Ini_Ranf_Quick: 826006868
|
---|
| 515 | | 0 -> 0.798860
|
---|
| 516 | | 1 -> 0.342478
|
---|
| 517 | | 2 -> 0.401300
|
---|
| 518 | | 3 -> 0.442912
|
---|
| 519 | | 4 -> 0.170912
|
---|
| 520 | --
|
---|
| 521 | */
|
---|
| 522 | /*
|
---|
| 523 | ++
|
---|
| 524 | | Auto_Ini_Ranf: date 887117207 s 188779 10^-6 sec seed=826007188:
|
---|
| 525 | | ... njours=10267 nj23=9 buf=826007188.77900004
|
---|
| 526 | | Ini_Ranf_Quick: 826007188
|
---|
| 527 | | 0 -> 0.455599
|
---|
| 528 | | 1 -> 0.811427
|
---|
| 529 | | 2 -> 0.703880
|
---|
| 530 | | 3 -> 0.409569
|
---|
| 531 | | 4 -> 0.390399
|
---|
| 532 | --
|
---|
| 533 | */
|
---|
| 534 | /*
|
---|
| 535 | ++
|
---|
| 536 | | Auto_Ini_Ranf: date 887117207 s 489750 10^-6 sec seed=826007489:
|
---|
| 537 | | ... njours=10267 nj23=9 buf=826007489.75
|
---|
| 538 | | Ini_Ranf_Quick: 826007489
|
---|
| 539 | | 0 -> 0.567094
|
---|
| 540 | | 1 -> 0.893156
|
---|
| 541 | | 2 -> 0.975995
|
---|
| 542 | | 3 -> 0.531331
|
---|
| 543 | | 4 -> 0.834354
|
---|
| 544 | --
|
---|
| 545 | */
|
---|
| 546 | /*==========================================================================*/
|
---|
| 547 |
|
---|
| 548 | /*==========================================================================*/
|
---|
| 549 | /*
|
---|
| 550 | ++
|
---|
| 551 | Module Tirages aleatoires selon une fonction (C)
|
---|
| 552 | Lib LibsUtil
|
---|
[3074] | 553 | include srandgen.h
|
---|
[851] | 554 | --
|
---|
| 555 | */
|
---|
| 556 | /*
|
---|
| 557 | ++
|
---|
| 558 | TIREALEA *init_tirage_alea(int nbin,double xmin,double xmax,double (*fonc) (double))
|
---|
| 559 | Initialise la structure qui va permettre le tirage aleatoire
|
---|
| 560 | d'un nombre compris entre xmin et xmax selon la
|
---|
| 561 | distribution fonc (histo de nbin bins)
|
---|
| 562 | --
|
---|
| 563 | */
|
---|
| 564 | TIREALEA *init_tirage_alea(int nbin,double xmin,double xmax,double (*fonc) (double))
|
---|
| 565 | {
|
---|
| 566 | int sof,i;
|
---|
| 567 | double x;
|
---|
| 568 | struct tirage_alea *t;
|
---|
| 569 |
|
---|
| 570 | if ( xmax-xmin<0.) return(NULL);
|
---|
| 571 |
|
---|
| 572 | if(nbin<=3) nbin=50;
|
---|
| 573 |
|
---|
| 574 | sof = sizeof(struct tirage_alea);
|
---|
| 575 | if( (t = (struct tirage_alea*)malloc(sof) ) == NULL ) {
|
---|
| 576 | printf("impossible d'allouer *tirage_alea par malloc \n");
|
---|
| 577 | return(NULL);
|
---|
| 578 | }
|
---|
| 579 |
|
---|
| 580 | t->Nbin=nbin; t->Min=xmin; t->Max=xmax; t->Lbin=(xmax-xmin) /nbin;
|
---|
| 581 |
|
---|
| 582 | sof = nbin * sizeof(double);
|
---|
| 583 | if( (t->Tab = (double*)malloc(sof) ) == NULL ) {
|
---|
| 584 | printf("impossible d'allouer *tirage_alea.Tab par malloc \n");
|
---|
| 585 | return(NULL);
|
---|
| 586 | }
|
---|
| 587 |
|
---|
| 588 | x = xmin + .5*t->Lbin;
|
---|
| 589 | t->Tab[0] = fonc(x);
|
---|
| 590 | for(i=1;i<nbin;i++) {
|
---|
| 591 | x = xmin + (i+.5)*t->Lbin;
|
---|
| 592 | t->Tab[i] = t->Tab[i-1] + fonc(x);
|
---|
| 593 | }
|
---|
| 594 |
|
---|
| 595 | for(i=0;i<nbin-1;i++) t->Tab[i] /= t->Tab[nbin-1];
|
---|
| 596 | t->Tab[nbin-1] = 1.;
|
---|
| 597 |
|
---|
| 598 | return(t);
|
---|
| 599 | }
|
---|
| 600 |
|
---|
| 601 | /*==========================================================================*/
|
---|
| 602 | /*
|
---|
| 603 | ++
|
---|
| 604 | double tirage_alea( TIREALEA *alea )
|
---|
| 605 | tirage aleatoire d'un nombre compris entre xmin et xmax
|
---|
| 606 | selon la fonction fonc (cf init_tirage_alea).
|
---|
| 607 | --
|
---|
| 608 | */
|
---|
| 609 | double tirage_alea( TIREALEA *alea )
|
---|
| 610 | {
|
---|
| 611 | int i,ibin = -1;
|
---|
| 612 | double z,t1,t2,x1,x2,t;
|
---|
| 613 |
|
---|
| 614 | z=drand01();
|
---|
| 615 | /* protections z<=0 ou z>=1 */
|
---|
| 616 | if( z <= 0. ) return ( alea->Min );
|
---|
| 617 | if( z >= 1. ) return ( alea->Max );
|
---|
| 618 | /* cas z <= tab[0] */
|
---|
| 619 | if(z <= alea->Tab[0]) {
|
---|
| 620 | t = alea->Min + (alea->Lbin/2.)/alea->Tab[0] * z;
|
---|
| 621 | return (t);
|
---|
| 622 | }
|
---|
| 623 |
|
---|
| 624 | /* recherche du premier bin plus grand que z */
|
---|
| 625 | for(i=0;i<alea->Nbin;i++) {
|
---|
| 626 | ibin=i;
|
---|
| 627 | if ( z < alea->Tab[i] ) break;
|
---|
| 628 | }
|
---|
| 629 |
|
---|
| 630 | /* extrapolation pour trouver la valeur du tirage aleatoire */
|
---|
| 631 | if( ibin == alea->Nbin-1 ) ibin--;
|
---|
| 632 | t1=alea->Tab[ibin];
|
---|
| 633 | x1 = alea->Min + (ibin+0.5) * alea->Lbin;
|
---|
| 634 | t2=alea->Tab[ibin+1];
|
---|
| 635 | x2 = x1 + alea->Lbin;
|
---|
| 636 | t = x1 + (x2-x1)/(t2-t1) *(z-t1);
|
---|
| 637 | if ( t < alea->Min ) t = alea->Min;
|
---|
| 638 | if ( t > alea->Max ) t = alea->Max;
|
---|
| 639 | return(t);
|
---|
| 640 | }
|
---|
| 641 |
|
---|
| 642 | /*==========================================================================*/
|
---|
| 643 | /*
|
---|
| 644 | ++
|
---|
| 645 | int end_tirage_alea( TIREALEA *alea )
|
---|
| 646 | De-allocation de la structure qui a permis le tirage aleatoire.
|
---|
| 647 | --
|
---|
| 648 | */
|
---|
| 649 | int end_tirage_alea( TIREALEA *alea )
|
---|
| 650 | {
|
---|
| 651 | if ( alea != NULL ) { free(alea); return(0);}
|
---|
| 652 | else return(-1);
|
---|
| 653 | }
|
---|