# HG changeset patch # User alpar # Date 1170789386 0 # Node ID ac0d843b8873cb82bb26966c110f75070b642ed1 # Parent 3609c77b77bebb6f98d31db7a59ec9bb72a6da83 Two new distributions added: - Gaussian distribution generated using the polar form of the Box-Muller transformation, - Exponential distribution generated using inverse cdf. diff -r 3609c77b77be -r ac0d843b8873 lemon/random.h --- a/lemon/random.h Tue Feb 06 19:09:17 2007 +0000 +++ b/lemon/random.h Tue Feb 06 19:16:26 2007 +0000 @@ -25,7 +25,6 @@ #include #include -#include ///\ingroup misc ///\file @@ -631,6 +630,38 @@ bool boolean(double p) { return operator()() < p; } + + /// Standard Gauss distribution + + /// Standard Gauss distribution. + /// \todo Currently it uses the so-calles "polar technique" to generate + /// random distribution. + /// Probably, the "Ziggurat" method should rather be used. + double gauss() + { + double V1,V2,S; + do { + V1=2*real()-1; + V2=2*real()-1; + S=V1*V1+V2*V2; + } while(S>=1); + return std::sqrt(-2*std::log(S)/S)*V1; + } + /// Gauss distribution with given variance and mean 0 + double gauss(double var) + { + return gauss()*var; + } + /// Gauss distribution with given variance and mean + double gauss(double var,double mean) + { + return gauss()*var+mean; + } + + double exponential(double lambda) + { + return -log(real())/lambda; + } };