Changeset 3099 in Sophya for trunk/SophyaLib/BaseTools


Ignore:
Timestamp:
Nov 2, 2006, 3:37:05 PM (19 years ago)
Author:
ansari
Message:

Ajout PoissRand() + classe RandomGenerator / doc ds srandgen.h .cc , Reza 02/11/2006

Location:
trunk/SophyaLib/BaseTools
Files:
2 edited

Legend:

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

    r3074 r3099  
    243243}
    244244
     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*/
     252int PoissRand(double mu)
     253{
     254  double pp,ppi;
     255  ppi = pp = exp(-mu);
     256  double x = drand01();
     257  int n = 0;
     258  while (x > ppi) {
     259    n++;
     260    pp = mu*pp/(double)n;
     261    ppi += pp;
     262  }
     263  return n;
     264}
     265
    245266/*=========================================================================*/
    246267/*
     
    250271        sigma "s" la distribution est limitee entre am +/- GAU_RANGE (obsolete).
    251272--
     273*/
     274/*! \ingroup  BaseTools
     275 \brief OBSOLETE (gaussian random number generator)
    252276*/
    253277double GauRnd1(double am, double s)
     
    371395}
    372396
     397
     398/*!
     399  \ingroup  BaseTools
     400  \class SOPHYA::RandomGenerator
     401  \brief Random number generator
     402 
     403  This is a class with static methods, providing an alternative interface
     404  to random number generations functions declared in srandgen.h.
     405
     406  \sa frand01 drand01 frandpm1 drandpm1
     407  \sa GauRnd PoissRand
     408  \sa Ini_Ranf_Quick Ini_Ranf Get_Ranf Auto_Ini_Ranf
     409*/
     410
    373411/*==========================================================================*/
    374412/*
  • trunk/SophyaLib/BaseTools/srandgen.h

    r2324 r3099  
    88#include "machdefs.h"
    99#include <stdlib.h>
    10 #ifdef __MWERKS__
    11 #include "unixmac.h"
    12 #endif
    1310
     11/* ------- SPECIFIQUE Mac Darwin OSX  <= 10.2 (gcc < 3.x) -------- */
    1412#if defined(Darwin) &&  defined(__GNUC__) && (__GNUC__ < 3)
    1513#include "osx_values.h"
     
    2725#define drand48() ((double)(random())/LONG_MAX)
    2826#endif
     27/* ------- FIN SPECIFIQUE Mac OSX / Darwin V <= 10.2 --------- */
    2928
     29#ifdef __cplusplus
     30/* Declaration en fonctions inlines pour du C++ */
     31inline float frand01() { return ( (float) drand48() ); }
     32inline double drand01() { return ( drand48() ); }
     33inline double ranf01() { return ( drand48() ); }
     34
     35inline float frandpm1() { return ( 2. * (float) drand48()  - 1.); }
     36inline double drandpm1() { return ( 2. * drand48() - 1.); }
     37inline double ranfpm1() { return ( 2. * drand48() - 1.); }
     38#else
     39/* les memes en define pour du C */
    3040#define frand01() ( (float) drand48() )
    3141#define drand01() ( drand48() )
     
    3545#define drandpm1() ( 2. * drand01() - 1.)
    3646#define ranfpm1() drandpm1()
     47#endif
    3748
    3849struct tirage_alea {
     
    6273            ,double mx,double my,double sa,double sb,double teta);
    6374
     75int PoissRand(double mu);
     76
    6477TIREALEA *init_tirage_alea(int nbin,double xmin,double xmax,double (*fonc) (double));
    6578double tirage_alea(TIREALEA *alea);
     
    7083#endif
    7184
     85/* -- Declaration d'une classe pour la centralier la doc -- Oct 2006 */
     86#ifdef __cplusplus
     87namespace SOPHYA {
     88class RandomGenerator {
     89 public:
     90  /*! \brief Automatic initialization through Auto_Ini_Ranf() */
     91  static inline void AutoInit(int lp=0) { Auto_Ini_Ranf(lp); return; }
     92  /*! \brief Return a random number \b r with a flat distribution  0<=r<=1 */
     93  static inline double Flat01() { return drand48(); }
     94  /*! \brief Return a random number \b r with a flat distribution  -1<=r<=1 */
     95  static inline double Flatpm1() { return drandpm1(); }
     96  /*! \brief Return a random number following a zero mean gaussian distribution  */
     97  static inline double Gaussian(double sigma=1.) { return GauRnd(0., sigma); }
     98  /*! \brief Return a random number following a Poisson distribution with mean \b mu  */
     99  static inline int    Poisson(double mu) { return PoissRand(mu); }
     100};
     101} /* namespace SOPHYA */
    72102#endif
    73103
     104#endif
     105
Note: See TracChangeset for help on using the changeset viewer.