Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Using the proper c++ way to get the min double value
[simgrid.git] / src / xbt / random.cpp
index 7f8af6a..03c544a 100644 (file)
@@ -5,6 +5,7 @@
 
 #include "xbt/random.hpp"
 #include "xbt/asserts.h"
+#include <limits>
 #include <random>
 
 namespace simgrid {
@@ -51,7 +52,12 @@ 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);
+  double u1                = numeratorA / divisor;
+  while (u1 < std::numeric_limits<double>::min()) {
+    numeratorA = mt19937_gen() - mt19937_gen.min();
+    u1         = numeratorA / divisor;
+  }
+  double z0 = sqrt(-2.0 * log(numeratorA / divisor)) * cos(2 * M_PI * numeratorB / divisor);
   return z0 * sd + mean;
 }