source: Sophya/trunk/SophyaLib/BaseTools/randinterf.h@ 3602

Last change on this file since 3602 was 3602, checked in by cmv, 16 years ago

RandomGeneratorInterface + dSFMT etc..., cmv 28/04/2009

File size: 3.5 KB
RevLine 
[3602]1#ifndef RANDOMGENERATORINTERFACE_H_SEEN
2#define RANDOMGENERATORINTERFACE_H_SEEN
3
4// Classes RandomGeneratorInterface
5//
6// R. Ansari (C) UPS+LAL IN2P3/CNRS 2009
7// C. Magneville (C) DAPNIA/SPP CEA 2009
8
9
10#include "machdefs.h"
11#include "anydataobj.h"
12#include <complex>
13#include <vector>
14
15namespace SOPHYA {
16
17//! Definition of the interface for Random number generator classes
18class RandomGeneratorInterface : public AnyDataObj {
19public:
20 RandomGeneratorInterface();
21 virtual ~RandomGeneratorInterface();
22
23 void UseGaussian(uint_2 type=0);
24 void UsePoisson(uint_2 type=0);
25 void UseExpo(uint_2 type=0);
26
27 void GenerateSeedVector(int nseed,vector<uint_2>& seed,int lp=0);
28
29 // --- Le tirage sur une distribution plate
30 /*! \brief Return a random number \b r with a flat distribution 0<=r<1 */
31 inline r_8 Flat01() {return Next();}
32 /*! \brief Return a random number \b r with a flat distribution -1<=r<1 */
33 inline r_8 Flatpm1() {return 2.*Next()-1.;}
34
35 // --- Le tirage sur une distribution gaussienne
36 /*! \brief Return a random number following a gaussian distribution (sigma=1, mean=0)*/
37 inline r_8 Gaussian()
38 {
39 if(usegaussian_==0) return GaussianBoxMuller();
40 if(usegaussian_==1) return GaussianSNorm();
41 if(usegaussian_==2) return GaussianPolarBoxMuller();
42 if(usegaussian_==3) return GaussianRatioUnif();
43 if(usegaussian_==4) return GaussianLevaRatioUnif();
44 return GaussianBoxMuller();
45 }
46 virtual r_8 GaussianBoxMuller();
47 virtual r_8 GaussianSNorm();
48 virtual r_8 GaussianPolarBoxMuller();
49 virtual r_8 GaussianRatioUnif();
50 virtual r_8 GaussianLevaRatioUnif();
51 /*! \brief Return a random number following a gaussian distribution "sigma", (mean=0)*/
52 inline r_8 Gaussian(double sigma) {return sigma*Gaussian();}
53 /*! \brief Return a random number following a gaussian distribution with mean=mu, and sigma */
54 inline r_8 Gaussian(double sigma,double mu) {return sigma*Gaussian()+mu;}
55
56 /*! \brief Return a random number following a gaussian tail distribution for x>sdev */
57 virtual r_8 GaussianTail(double sdev);
58
59 // --- Le tirage sur une distribution de poisson
60 /*! \brief Return a random number following a poisson distribution with mean mu */
61 inline uint_8 Poisson(double mu, double mumax)
62 {
63 if(usepoisson_==0) return PoissonSimple(mu,mumax);
64 if(usepoisson_==1) return PoissonAhrens(mu);
65 return PoissonSimple(mu,mumax);
66 }
67 virtual uint_8 PoissonSimple(double mu, double mumax=-1);
68 virtual uint_8 PoissonAhrens(double mu);
69
70 // --- Le tirage sur une distribution exponentielle
71 inline r_8 Expo()
72 {
73 if(useexpo_==0) return ExpoSimple();
74 if(useexpo_==1) return ExpoAhrens();
75 return ExpoSimple();
76 }
77 virtual r_8 ExpoSimple();
78 virtual r_8 ExpoAhrens();
79
80 // --- Le tirage gaussien complexe (cf texte a la fin du .cc)
81 inline complex< r_8 > ComplexGaussRan(void)
82 {return complex< r_8 >(Gaussian(),Gaussian());}
83 inline complex< r_8 > ComplexGaussRan(double sig)
84 {return complex< r_8 >(sig*Gaussian(),sig*Gaussian());}
85 /*! \brief Returns the module of a random complex number generated by ComplexGaussRan */
86 inline double ModComplexGaussRan(double sig=1.)
87 {double r=-log(1.-Next()); return sig*sqrt(2.*r);}
88
89protected:
90 //! Return a random number in [0,1]
91 virtual r_8 Next();
92 uint_2 usegaussian_, usepoisson_, useexpo_;
93
94};
95
96} /* namespace SOPHYA */
97
98#endif
Note: See TracBrowser for help on using the repository browser.