From: Yann Duplouy Date: Wed, 27 Nov 2019 15:07:43 +0000 (+0100) Subject: Redraw if the random number is too close to max, to avoid modulo bias X-Git-Tag: v3.26~1027^2~1 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/a5a63cee9b36d804e5442b421f1c3c3562b7a8ac Redraw if the random number is too close to max, to avoid modulo bias See framagit issue 43. --- 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; }