Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Exclude max from range for xbt::random::uniform_real.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 6 Feb 2020 10:18:05 +0000 (11:18 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 6 Feb 2020 10:45:02 +0000 (11:45 +0100)
Mimic std::uniform_real_distribution.

include/xbt/random.hpp
src/xbt/random.cpp
src/xbt/random_test.cpp

index b67487d..c55ba8b 100644 (file)
@@ -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
index beaaf21..22d7591 100644 (file)
@@ -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;
 }
index b7c10e8..f601ea9 100644 (file)
@@ -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")