[Lemon-commits] alpar: r3155 - hugo/trunk/lemon
Lemon SVN
svn at lemon.cs.elte.hu
Tue Feb 6 20:16:27 CET 2007
Author: alpar
Date: Tue Feb 6 20:16:26 2007
New Revision: 3155
Modified:
hugo/trunk/lemon/random.h
Log:
Two new distributions added:
- Gaussian distribution generated using the polar form of the Box-Muller
transformation,
- Exponential distribution generated using inverse cdf.
Modified: hugo/trunk/lemon/random.h
==============================================================================
--- hugo/trunk/lemon/random.h (original)
+++ hugo/trunk/lemon/random.h Tue Feb 6 20:16:26 2007
@@ -25,7 +25,6 @@
#include <ctime>
#include <cmath>
-#include <cmath>
///\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<double>()-1;
+ V2=2*real<double>()-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<double>())/lambda;
+ }
};
More information about the Lemon-commits
mailing list