[1022] | 1 | // -*- C++ -*- |
---|
| 2 | // $Id$ |
---|
| 3 | |
---|
| 4 | /* |
---|
| 5 | * This generator uses the straightforward transformation |
---|
| 6 | * x = - log(y) * m |
---|
| 7 | * |
---|
| 8 | * to turn a uniform (0,1) y into an exponentially distributed |
---|
| 9 | * variable x. x has density function |
---|
| 10 | * |
---|
| 11 | * f(x) = (1/m) exp(-(1/m)x) (x > 0) |
---|
| 12 | * |
---|
| 13 | * and mean m. |
---|
| 14 | * |
---|
| 15 | * NEEDS_WORK: Adapt the method of Ahrens and Dieter. This will |
---|
| 16 | * require extending the precision of the constants. |
---|
| 17 | * |
---|
| 18 | * Ahrens, J.H. and Dieter, U. Computer Methods for Sampling From the |
---|
| 19 | * Exponential and Normal Distributions. Comm. ACM, 15,10 (Oct. 1972), p. 873. |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #ifndef BZ_RANDOM_EXPONENTIAL |
---|
| 23 | #define BZ_RANDOM_EXPONENTIAL |
---|
| 24 | |
---|
| 25 | #ifndef BZ_RANDOM_UNIFORM |
---|
| 26 | #include <random/uniform.h> |
---|
| 27 | #endif |
---|
| 28 | |
---|
| 29 | BZ_NAMESPACE(ranlib) |
---|
| 30 | |
---|
| 31 | template<typename T = double, typename IRNG = defaultIRNG, |
---|
| 32 | typename stateTag = defaultState> |
---|
| 33 | class ExponentialUnit : public UniformOpen<T,IRNG,stateTag> |
---|
| 34 | { |
---|
| 35 | public: |
---|
| 36 | typedef T T_numtype; |
---|
| 37 | |
---|
| 38 | ExponentialUnit() {} |
---|
| 39 | |
---|
| 40 | explicit ExponentialUnit(unsigned int i) : |
---|
| 41 | UniformOpen<T,IRNG,stateTag>(i) {}; |
---|
| 42 | |
---|
| 43 | T random() |
---|
| 44 | { |
---|
| 45 | return - log(UniformOpen<T,IRNG,stateTag>::random()); |
---|
| 46 | } |
---|
| 47 | }; |
---|
| 48 | |
---|
| 49 | template<typename T = double, typename IRNG = defaultIRNG, |
---|
| 50 | typename stateTag = defaultState> |
---|
| 51 | class Exponential : public ExponentialUnit<T,IRNG,stateTag> { |
---|
| 52 | |
---|
| 53 | public: |
---|
| 54 | typedef T T_numtype; |
---|
| 55 | |
---|
| 56 | Exponential(T mean) |
---|
| 57 | { |
---|
| 58 | mean_ = mean; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | Exponential(T mean, unsigned int i) : |
---|
| 62 | UniformOpen<T,IRNG,stateTag>(i) |
---|
| 63 | { |
---|
| 64 | mean_ = mean; |
---|
| 65 | }; |
---|
| 66 | |
---|
| 67 | T random() |
---|
| 68 | { |
---|
| 69 | return mean_ * ExponentialUnit<T,IRNG,stateTag>::random(); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | private: |
---|
| 73 | T mean_; |
---|
| 74 | }; |
---|
| 75 | |
---|
| 76 | BZ_NAMESPACE_END |
---|
| 77 | |
---|
| 78 | #endif // BZ_RANDOM_EXPONENTIAL |
---|