Changeset 3364 in Sophya for trunk/SophyaLib/BaseTools/srandgen.c


Ignore:
Timestamp:
Oct 29, 2007, 6:34:27 PM (18 years ago)
Author:
cmv
Message:

add PoissRandLimit pour tirage sur distrib poisson avec protection, cmv 29/10/2007

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/BaseTools/srandgen.c

    r3102 r3364  
    254254  double pp,ppi,x;
    255255  int n;
     256  ppi = pp = exp(-mu);
     257  x = drand01();
     258  n = 0;
     259  while (x > ppi) {
     260    n++;
     261    pp = mu*pp/(double)n;
     262    ppi += pp;
     263  }
     264  return n;
     265}
     266
     267/*! \ingroup  BaseTools
     268  \brief Poisson random number generator with approximation for large mean.
     269 
     270  Return an integer value (>=0) corresponding a Poisson distribution with mean \b mu.
     271  If \b mu is greater or equal than \b mumax,
     272  the large number gaussian approximation is applied.
     273  \b mumax=10 is a good value.
     274*/
     275unsigned long PoissRandLimit(double mu,double mumax)
     276// p(n) = (mu^n / n!) exp(-mu)  for n = 0, 1, 2, ...
     277{
     278  double pp,ppi,x;
     279  unsigned long n;
     280
     281  if(mu>=mumax) {
     282    pp = sqrt(mu);
     283    while( (x=pp*NorRand()) < -mu );
     284    return (unsigned long)(mu+x+0.5);
     285  }
     286
    256287  ppi = pp = exp(-mu);
    257288  x = drand01();
Note: See TracChangeset for help on using the changeset viewer.