From: Arnaud Giersch Date: Thu, 6 Feb 2020 10:18:05 +0000 (+0100) Subject: Exclude max from range for xbt::random::uniform_real. X-Git-Tag: v3.26~1025 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/037217ec2176ada8eb05cd8e5f5b63e7440c63a3 Exclude max from range for xbt::random::uniform_real. Mimic std::uniform_real_distribution. --- diff --git a/include/xbt/random.hpp b/include/xbt/random.hpp index b67487d623..c55ba8b713 100644 --- a/include/xbt/random.hpp +++ b/include/xbt/random.hpp @@ -26,7 +26,7 @@ void set_implem_std(); void set_mersenne_seed(int); /** - * @brief Draws an integer number uniformly between min and max included + * @brief Draws an integer number uniformly in range [min, max] (min and max included) * * @param min Minimum value * @param max Maximum value @@ -34,7 +34,7 @@ void set_mersenne_seed(int); int uniform_int(int min, int max); /** - * @brief Draws a real number uniformly between min and max included + * @brief Draws a real number uniformly in range [min, max) (min included, and max excluded) * * @param min Minimum value * @param max Maximum value diff --git a/src/xbt/random.cpp b/src/xbt/random.cpp index beaaf21329..22d7591650 100644 --- a/src/xbt/random.cpp +++ b/src/xbt/random.cpp @@ -55,7 +55,7 @@ double uniform_real(double min, double max) } // This reuses Boost's uniform real distribution ideas - constexpr unsigned long divisor = decltype(mt19937_gen)::max() - decltype(mt19937_gen)::min(); + constexpr unsigned long divisor = decltype(mt19937_gen)::max() - decltype(mt19937_gen)::min() + 1; unsigned long numerator = mt19937_gen() - decltype(mt19937_gen)::min(); return min + (max - min) * numerator / divisor; } diff --git a/src/xbt/random_test.cpp b/src/xbt/random_test.cpp index b7c10e8a2c..f601ea9791 100644 --- a/src/xbt/random_test.cpp +++ b/src/xbt/random_test.cpp @@ -16,10 +16,10 @@ TEST_CASE("xbt::random: Random Number Generation") SECTION("Using XBT_RNG_xbt") { simgrid::xbt::random::set_mersenne_seed(12345); - REQUIRE_THAT(simgrid::xbt::random::exponential(25), EpsilonApprox(0.00291934351538427348)); + REQUIRE_THAT(simgrid::xbt::random::exponential(25), EpsilonApprox(0.00291934352469749815)); REQUIRE(simgrid::xbt::random::uniform_int(1, 6) == 4); - REQUIRE_THAT(simgrid::xbt::random::uniform_real(0, 1), EpsilonApprox(0.31637556043369124970)); - REQUIRE_THAT(simgrid::xbt::random::normal(0, 2), EpsilonApprox(1.62746784745133976635)); + REQUIRE_THAT(simgrid::xbt::random::uniform_real(0, 1), EpsilonApprox(0.31637556036002933979)); + REQUIRE_THAT(simgrid::xbt::random::normal(0, 2), EpsilonApprox(1.62746784853777226587)); } SECTION("Using XBT_RNG_std")