Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Replace usage of <random> with <xbt/random.hpp>.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Wed, 5 Feb 2020 22:45:20 +0000 (23:45 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 6 Feb 2020 14:46:58 +0000 (15:46 +0100)
examples/s4u/energy-link/s4u-energy-link.cpp
src/instr/instr_interface.cpp
src/mc/mc_base.cpp
src/mc/sosp/Snapshot_test.cpp
src/xbt/dict_test.cpp
teshsuite/surf/wifi_usage/wifi_usage.cpp

index eb7d36a..cd0c5fb 100644 (file)
@@ -5,13 +5,12 @@
 
 #include "simgrid/plugins/energy.h"
 #include "xbt/log.h"
+#include "xbt/random.hpp"
 #include <simgrid/s4u.hpp>
 
-#include <random>
-
 /* Parameters of the random generation of the flow size */
-static const unsigned long int min_size = 1e6;
-static const unsigned long int max_size = 1e9;
+static const int min_size = 1e6;
+static const int max_size = 1e9;
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_app_energyconsumption, "Messages specific for this s4u example");
 
@@ -93,16 +92,8 @@ int main(int argc, char* argv[])
 
   if (argc > 3) {
     if (strcmp(argv[3], "random") == 0) { // We're asked to get a random size
-      /* Initialize the random number generator */
-      std::random_device rd;
-      std::default_random_engine generator(rd());
-
-      /* Distribution on which to apply the generator */
-      std::uniform_int_distribution<unsigned long int> distribution(min_size, max_size);
-
-      char* size = bprintf("%lu", distribution(generator));
-      argSender.push_back(std::string(size));
-      xbt_free(size);
+      std::string size = std::to_string(simgrid::xbt::random::uniform_int(min_size, max_size));
+      argSender.push_back(size);
     } else {                        // Not "random" ? Then it should be the size to use
       argSender.push_back(argv[3]); // Take the datasize from the command line
     }
index 824af31..963a71e 100644 (file)
@@ -9,9 +9,9 @@
 #include "src/surf/network_interface.hpp"
 #include "src/surf/surf_private.hpp"
 #include "surf/surf.hpp"
+#include "xbt/random.hpp"
 #include <algorithm>
 #include <cmath>
-#include <random>
 
 enum class InstrUserVariable { DECLARE, SET, ADD, SUB };
 
@@ -89,11 +89,9 @@ void TRACE_category_with_color (const char *category, const char *color)
   std::string final_color;
   if (not color) {
     //generate a random color
-    static std::default_random_engine rnd_engine;
-    std::uniform_real_distribution<double> prng(0.0, std::nextafter(1.0, 2.0));
-    double red   = prng(rnd_engine);
-    double green = prng(rnd_engine);
-    double blue  = prng(rnd_engine);
+    double red   = simgrid::xbt::random::uniform_real(0.0, std::nextafter(1.0, 2.0));
+    double green = simgrid::xbt::random::uniform_real(0.0, std::nextafter(1.0, 2.0));
+    double blue  = simgrid::xbt::random::uniform_real(0.0, std::nextafter(1.0, 2.0));
     final_color  = std::to_string(red) + " " + std::to_string(green) + " " + std::to_string(blue);
   }else{
     final_color = std::string(color);
index b76dc7c..3656982 100644 (file)
@@ -11,7 +11,7 @@
 #include "src/mc/mc_replay.hpp"
 #include "src/simix/smx_private.hpp"
 
-#include <random>
+#include "xbt/random.hpp"
 
 #if SIMGRID_HAVE_MC
 #include "src/mc/ModelChecker.hpp"
@@ -166,9 +166,8 @@ bool request_is_visible(const s_smx_simcall* req)
 int simcall_HANDLER_mc_random(smx_simcall_t simcall, int min, int max)
 {
   if (not MC_is_active() && MC_record_path.empty()) {
-    static std::default_random_engine rnd_engine;
-    std::uniform_int_distribution<int> prng(min, max);
-    return prng(rnd_engine);
+    static simgrid::xbt::random::XbtRandom prng;
+    return prng.uniform_int(min, max);
   }
   return simcall->mc_value_;
 }
index 36a7298..f78365f 100644 (file)
@@ -8,8 +8,8 @@
 #include "src/mc/sosp/Snapshot.hpp"
 
 #include <cstddef>
-#include <random>
 #include <sys/mman.h>
+#include <xbt/random.hpp>
 
 /**************** Class BOOST_tests *************************/
 using simgrid::mc::Region;
@@ -37,19 +37,17 @@ public:
     mc_model_checker = nullptr;
   }
 
-  static std::default_random_engine rnd_engine;
   static std::unique_ptr<simgrid::mc::RemoteClient> process;
 };
 
 // static member variables init.
-std::default_random_engine snap_test_helper::rnd_engine;
 std::unique_ptr<simgrid::mc::RemoteClient> snap_test_helper::process = nullptr;
 
 void snap_test_helper::init_memory(void* mem, size_t size)
 {
   char* dest = (char*)mem;
   for (size_t i = 0; i < size; ++i) {
-    dest[i] = rnd_engine() & 255;
+    dest[i] = simgrid::xbt::random::uniform_int(0, 0xff);
   }
 }
 
@@ -111,8 +109,8 @@ void snap_test_helper::read_region_parts()
     prologue_return ret = prologue(n);
 
     for (int j = 0; j != 100; ++j) {
-      size_t offset    = rnd_engine() % ret.size;
-      size_t size      = rnd_engine() % (ret.size - offset);
+      size_t offset    = simgrid::xbt::random::uniform_int(0, ret.size - 1);
+      size_t size      = simgrid::xbt::random::uniform_int(0, ret.size - offset - 1);
       const void* read = ret.region->read(ret.dstn, (const char*)ret.src + offset, size);
       INFO("Mismatch in MC_region_read()");
       REQUIRE(not memcmp((char*)ret.src + offset, read, size));
@@ -145,8 +143,8 @@ void snap_test_helper::compare_region_parts()
     prologue_return ret = prologue(n);
 
     for (int j = 0; j != 100; ++j) {
-      size_t offset = rnd_engine() % ret.size;
-      size_t size   = rnd_engine() % (ret.size - offset);
+      size_t offset = simgrid::xbt::random::uniform_int(0, ret.size - 1);
+      size_t size   = simgrid::xbt::random::uniform_int(0, ret.size - offset - 1);
 
       INFO("Mismatch in MC_snapshot_region_memcmp()");
       REQUIRE(not MC_snapshot_region_memcmp((char*)ret.src + offset, ret.region, (char*)ret.src + offset, ret.region,
index ed105ea..5891394 100644 (file)
@@ -8,9 +8,9 @@
 #include "xbt/dict.h"
 
 #include "simgrid/Exception.hpp"
+#include "xbt/random.hpp"
 #include <cstdio>
 #include <cstring>
-#include <random>
 
 #include "catch.hpp"
 
@@ -264,9 +264,6 @@ TEST_CASE("xbt::dict: dict data container", "dict")
 
   SECTION("Crash test")
   {
-    std::random_device rd;
-    std::default_random_engine rnd_engine(rd());
-
     for (int i = 0; i < 10; i++) {
       INFO("CRASH test number " << i + 1 << " (" << 10 - i - 1 << " to go)");
       INFO("Fill the struct, count its elems and frees the structure");
@@ -278,7 +275,7 @@ TEST_CASE("xbt::dict: dict data container", "dict")
 
         do {
           for (int k = 0; k < SIZEOFKEY - 1; k++) {
-            key[k] = rnd_engine() % ('z' - 'a') + 'a';
+            key[k] = simgrid::xbt::random::uniform_int('a', 'z');
           }
           key[SIZEOFKEY - 1] = '\0';
           data               = (char*)xbt_dict_get_or_null(head, key);
index d6929d6..1d5e849 100644 (file)
@@ -8,10 +8,6 @@
 
 #include "simgrid/msg.h"
 #include "src/surf/network_wifi.hpp"
-#include <exception>
-#include <iostream>
-#include <random>
-#include <string>
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(simulator, "[usage] wifi_usage <platform-file>");