Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Redraw if the random number is too close to max, to avoid modulo bias
authorYann Duplouy <yann.duplouy@inria.fr>
Wed, 27 Nov 2019 15:07:43 +0000 (16:07 +0100)
committerYann Duplouy <yann.duplouy@inria.fr>
Wed, 27 Nov 2019 15:24:08 +0000 (16:24 +0100)
See framagit issue 43.

src/xbt/random.cpp

index e7ba9e2..1886dbd 100644 (file)
@@ -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;
 }