X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/90680837ebef1bd470bd846a684b28a7aff1ab90..0ffe6c466e437ebbda9be47bbd77d3775623a2d7:/src/xbt/random.cpp diff --git a/src/xbt/random.cpp b/src/xbt/random.cpp index 78d115a814..088545eb5f 100644 --- a/src/xbt/random.cpp +++ b/src/xbt/random.cpp @@ -65,14 +65,20 @@ double StdRandom::normal(double mean, double sd) int XbtRandom::uniform_int(int min, int max) { - unsigned long range = max - min + 1; + unsigned long range = static_cast(max) - static_cast(min); xbt_assert(min <= max, "The minimum value for the uniform integer distribution must not be greater than the maximum value"); - xbt_assert(range > 0, "Overflow in the uniform integer distribution, please use a smaller range."); + xbt_assert(range <= decltype(mt19937_gen)::max(), + "Overflow in the uniform integer distribution, please use a smaller range."); + if (range == decltype(mt19937_gen)::max()) + return static_cast(mt19937_gen() + min); + + ++range; + unsigned long limit = decltype(mt19937_gen)::max() - decltype(mt19937_gen)::max() % range; unsigned long value; do { value = mt19937_gen(); - } while (value >= decltype(mt19937_gen)::max() - decltype(mt19937_gen)::max() % range); + } while (value >= limit); return static_cast(value % range + min); }