From 15f7e082b6a5831848150c7a95dab8b60b99ecce Mon Sep 17 00:00:00 2001 From: mlaurent Date: Wed, 7 Jun 2023 15:26:35 +0200 Subject: [PATCH 1/1] Use xbt::random instead of rand and srand --- src/mc/api/State.cpp | 6 ++++-- src/mc/api/strategy/UniformStrategy.hpp | 7 ++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/mc/api/State.cpp b/src/mc/api/State.cpp index cc85d8c5fb..0b6fc02344 100644 --- a/src/mc/api/State.cpp +++ b/src/mc/api/State.cpp @@ -10,6 +10,7 @@ #include "src/mc/api/strategy/UniformStrategy.hpp" #include "src/mc/explo/Exploration.hpp" #include "src/mc/mc_config.hpp" +#include "xbt/random.hpp" #include #include @@ -24,7 +25,6 @@ State::State(RemoteApp& remote_app) : num_(++expended_states_) { XBT_VERB("Creating a guide for the state"); - srand(_sg_mc_random_seed); if (_sg_mc_strategy == "none") strategy_ = std::make_shared(); @@ -32,8 +32,10 @@ State::State(RemoteApp& remote_app) : num_(++expended_states_) strategy_ = std::make_shared(); else if (_sg_mc_strategy == "min_match_comm") strategy_ = std::make_shared(); - else if (_sg_mc_strategy == "uniform") + else if (_sg_mc_strategy == "uniform") { + xbt::random::set_mersenne_seed(_sg_mc_random_seed); strategy_ = std::make_shared(); + } else THROW_IMPOSSIBLE; diff --git a/src/mc/api/strategy/UniformStrategy.hpp b/src/mc/api/strategy/UniformStrategy.hpp index 1dfcd07939..8e9726f44a 100644 --- a/src/mc/api/strategy/UniformStrategy.hpp +++ b/src/mc/api/strategy/UniformStrategy.hpp @@ -7,6 +7,7 @@ #define SIMGRID_MC_UNIFORMSTRATEGY_HPP #include "src/mc/transition/Transition.hpp" +#include "xbt/random.hpp" #define MAX_RAND 100000 @@ -20,12 +21,12 @@ public: UniformStrategy() { for (long aid = 0; aid < 10; aid++) - valuation[aid] = rand() % 10000; + valuation[aid] = xbt::random::uniform_int(0, MAX_RAND); } void copy_from(const Strategy* strategy) override { for (auto& [aid, _] : actors_to_run_) - valuation[aid] = rand() % 10000; + valuation[aid] = xbt::random::uniform_int(0, MAX_RAND); } std::pair best_transition(bool must_be_todo) const override @@ -44,7 +45,7 @@ public: if (possibilities == 1) chosen = 0; else - chosen = rand() % possibilities; + chosen = xbt::random::uniform_int(0, possibilities-1); for (auto const& [aid, actor] : actors_to_run_) { if (((not actor.is_todo()) and must_be_todo) or actor.is_done() or (not actor.is_enabled())) -- 2.20.1