Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Adding the normal distribution
authorYann Duplouy <yann.duplouy@inria.fr>
Mon, 21 Oct 2019 09:41:50 +0000 (11:41 +0200)
committerYann Duplouy <yann.duplouy@inria.fr>
Mon, 21 Oct 2019 09:41:50 +0000 (11:41 +0200)
include/xbt/random.hpp
src/xbt/random.cpp

index 84e8182..cf2b168 100644 (file)
@@ -14,6 +14,7 @@ namespace random {
 int uniform_int(int, int);
 double uniform_real(double, double);
 double exponential(double);
+double normal(double, double);
 } // namespace random
 } // namespace xbt
 } // namespace simgrid
index 95aed12..7f8af6a 100644 (file)
@@ -46,6 +46,15 @@ double exponential(double lambda)
   return -1 / lambda * log(numerator / divisor);
 }
 
+double normal(double mean, double sd)
+{
+  unsigned long numeratorA = mt19937_gen() - mt19937_gen.min();
+  unsigned long numeratorB = mt19937_gen() - mt19937_gen.min();
+  unsigned long divisor    = mt19937_gen.max() - mt19937_gen.min();
+  double z0                = sqrt(-2.0 * log(numeratorA / divisor)) * cos(2 * M_PI * numeratorB / divisor);
+  return z0 * sd + mean;
+}
+
 } // namespace random
 } // namespace xbt
 } // namespace simgrid