Two new distributions added:
authoralpar
Tue, 06 Feb 2007 19:16:26 +0000
changeset 2355ac0d843b8873
parent 2354 3609c77b77be
child 2356 57c316cb868b
Two new distributions added:
- Gaussian distribution generated using the polar form of the Box-Muller
transformation,
- Exponential distribution generated using inverse cdf.
lemon/random.h
     1.1 --- a/lemon/random.h	Tue Feb 06 19:09:17 2007 +0000
     1.2 +++ b/lemon/random.h	Tue Feb 06 19:16:26 2007 +0000
     1.3 @@ -25,7 +25,6 @@
     1.4  
     1.5  #include <ctime>
     1.6  #include <cmath>
     1.7 -#include <cmath>
     1.8  
     1.9  ///\ingroup misc
    1.10  ///\file
    1.11 @@ -631,6 +630,38 @@
    1.12      bool boolean(double p) {
    1.13        return operator()() < p;
    1.14      }
    1.15 +
    1.16 +    /// Standard Gauss distribution
    1.17 +
    1.18 +    /// Standard Gauss distribution.
    1.19 +    /// \todo Currently it uses the so-calles "polar technique" to generate
    1.20 +    /// random distribution.
    1.21 +    /// Probably, the "Ziggurat" method should rather be used.
    1.22 +    double gauss() 
    1.23 +    {
    1.24 +      double V1,V2,S;
    1.25 +      do {
    1.26 +	V1=2*real<double>()-1;
    1.27 +	V2=2*real<double>()-1;
    1.28 +	S=V1*V1+V2*V2;
    1.29 +      } while(S>=1);
    1.30 +      return std::sqrt(-2*std::log(S)/S)*V1;
    1.31 +    }
    1.32 +    /// Gauss distribution with given variance and mean 0
    1.33 +    double gauss(double var) 
    1.34 +    {
    1.35 +      return gauss()*var;
    1.36 +    }
    1.37 +    /// Gauss distribution with given variance and mean
    1.38 +    double gauss(double var,double mean)
    1.39 +    {
    1.40 +      return gauss()*var+mean;
    1.41 +    }
    1.42 +
    1.43 +    double exponential(double lambda)
    1.44 +    {
    1.45 +      return -log(real<double>())/lambda;
    1.46 +    }
    1.47      
    1.48    };
    1.49