From a5a63cee9b36d804e5442b421f1c3c3562b7a8ac Mon Sep 17 00:00:00 2001 From: Yann Duplouy Date: Wed, 27 Nov 2019 16:07:43 +0100 Subject: [PATCH 1/1] Redraw if the random number is too close to max, to avoid modulo bias See framagit issue 43. --- src/xbt/random.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/xbt/random.cpp b/src/xbt/random.cpp index e7ba9e28b5..1886dbd54c 100644 --- a/src/xbt/random.cpp +++ b/src/xbt/random.cpp @@ -42,6 +42,9 @@ int uniform_int(int min, int max) xbt_assert( min <= max, "The maximum value for the uniform integer distribution must be greater than or equal to the minimum value"); + while (value >= mt19937_gen.max() - mt19937_gen.max() % range) { + value = mt19937_gen(); + } return value % range + min; } -- 2.20.1