Changeset 3099 in Sophya
- Timestamp:
- Nov 2, 2006, 3:37:05 PM (19 years ago)
- Location:
- trunk/SophyaLib/BaseTools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/BaseTools/srandgen.c
r3074 r3099 243 243 } 244 244 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 { 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 245 266 /*=========================================================================*/ 246 267 /* … … 250 271 sigma "s" la distribution est limitee entre am +/- GAU_RANGE (obsolete). 251 272 -- 273 */ 274 /*! \ingroup BaseTools 275 \brief OBSOLETE (gaussian random number generator) 252 276 */ 253 277 double GauRnd1(double am, double s) … … 371 395 } 372 396 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 373 411 /*==========================================================================*/ 374 412 /* -
trunk/SophyaLib/BaseTools/srandgen.h
r2324 r3099 8 8 #include "machdefs.h" 9 9 #include <stdlib.h> 10 #ifdef __MWERKS__11 #include "unixmac.h"12 #endif13 10 11 /* ------- SPECIFIQUE Mac Darwin OSX <= 10.2 (gcc < 3.x) -------- */ 14 12 #if defined(Darwin) && defined(__GNUC__) && (__GNUC__ < 3) 15 13 #include "osx_values.h" … … 27 25 #define drand48() ((double)(random())/LONG_MAX) 28 26 #endif 27 /* ------- FIN SPECIFIQUE Mac OSX / Darwin V <= 10.2 --------- */ 29 28 29 #ifdef __cplusplus 30 /* Declaration en fonctions inlines pour du C++ */ 31 inline float frand01() { return ( (float) drand48() ); } 32 inline double drand01() { return ( drand48() ); } 33 inline double ranf01() { return ( drand48() ); } 34 35 inline float frandpm1() { return ( 2. * (float) drand48() - 1.); } 36 inline double drandpm1() { return ( 2. * drand48() - 1.); } 37 inline double ranfpm1() { return ( 2. * drand48() - 1.); } 38 #else 39 /* les memes en define pour du C */ 30 40 #define frand01() ( (float) drand48() ) 31 41 #define drand01() ( drand48() ) … … 35 45 #define drandpm1() ( 2. * drand01() - 1.) 36 46 #define ranfpm1() drandpm1() 47 #endif 37 48 38 49 struct tirage_alea { … … 62 73 ,double mx,double my,double sa,double sb,double teta); 63 74 75 int PoissRand(double mu); 76 64 77 TIREALEA *init_tirage_alea(int nbin,double xmin,double xmax,double (*fonc) (double)); 65 78 double tirage_alea(TIREALEA *alea); … … 70 83 #endif 71 84 85 /* -- Declaration d'une classe pour la centralier la doc -- Oct 2006 */ 86 #ifdef __cplusplus 87 namespace SOPHYA { 88 class 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 */ 72 102 #endif 73 103 104 #endif 105
Note:
See TracChangeset
for help on using the changeset viewer.