Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Redraw if the random number is too close to max, to avoid modulo bias
[simgrid.git] / 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;
 }