From af116ea50535b17960c8c7d963fe9485787b2651 Mon Sep 17 00:00:00 2001 From: Yann Duplouy Date: Mon, 21 Oct 2019 11:42:49 +0200 Subject: [PATCH] Adding the normal distribution --- src/xbt/random.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/xbt/random.cpp b/src/xbt/random.cpp index 7f8af6affb..b0f172a290 100644 --- a/src/xbt/random.cpp +++ b/src/xbt/random.cpp @@ -51,7 +51,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 < DBL_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; } -- 2.20.1