#define SIMGRID_XBT_RANDOM_HPP
#include "xbt/base.h"
+#include <fstream>
+#include <iostream>
#include <random>
+#include <string>
namespace simgrid {
namespace xbt {
*/
void set_seed(int seed) { mt19937_gen.seed(seed); }
+ /**
+ * @brief Read the state of the Mersenne-Twister RNG from a file
+ */
+ bool read_state(std::string filename);
+
+ /**
+ * @brief Write the state of the Mersenne-Twister RNG to a file
+ */
+ bool write_state(std::string filename);
+
/**
* @brief Draws an integer number uniformly in range [min, max] (min and max included)
*
*/
void set_mersenne_seed(int);
+/**
+ * @brief Read the state of the Mersenne-Twister RNG from a file.
+ */
+bool read_mersenne_state(std::string filename);
+
+/**
+ * @brief Write the state of the Mersenne-Twister RNG to a file.
+ */
+bool write_mersenne_state(std::string filename);
+
/**
* @brief Draws an integer number uniformly in range [min, max] (min and max included)
*
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
-#include "xbt/random.hpp"
#include "xbt/asserts.h"
+#include <fstream>
+#include <iostream>
#include <limits>
#include <memory>
+#include <string>
+#include <xbt/log.hpp>
+#include <xbt/random.hpp>
+
+XBT_LOG_EXTERNAL_CATEGORY(xbt);
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_random, xbt, "Random");
namespace simgrid {
namespace xbt {
namespace random {
+bool Random::read_state(std::string filename)
+{
+ std::ifstream file(filename);
+ if (file) {
+ file >> mt19937_gen;
+ return true;
+ } else {
+ XBT_WARN("Could not open %s and thus not save the RNG state.", filename.c_str());
+ return false;
+ }
+}
+
+bool Random::write_state(std::string filename)
+{
+ std::ofstream file(filename);
+ if (file) {
+ file << mt19937_gen;
+ return true;
+ } else {
+ XBT_WARN("Could not open %s and thus not read the RNG state.", filename.c_str());
+ return false;
+ }
+}
+
int StdRandom::uniform_int(int min, int max)
{
std::uniform_int_distribution<> dist(min, max);
default_random->set_seed(seed);
}
+bool read_mersenne_state(std::string filename)
+{
+ return default_random->read_state(filename);
+}
+
+bool write_mersenne_state(std::string filename)
+{
+ return default_random->write_state(filename);
+}
+
int uniform_int(int min, int max)
{
return default_random->uniform_int(min, max);
REQUIRE_THAT(simgrid::xbt::random::uniform_real(0, 1), EpsilonApprox(distC(gen)));
REQUIRE_THAT(simgrid::xbt::random::normal(0, 2), EpsilonApprox(distD(gen)));
}
+
+ SECTION("XBT_RNG_std write to a file")
+ {
+ simgrid::xbt::random::set_implem_std();
+ simgrid::xbt::random::set_mersenne_seed(12345);
+
+ simgrid::xbt::random::exponential(25);
+ bool writtenA = simgrid::xbt::random::write_mersenne_state("rdm_state_tmp.txt");
+ double resB = simgrid::xbt::random::uniform_real(10, 20);
+ double resC = simgrid::xbt::random::normal(0, 2);
+ bool writtenB = simgrid::xbt::random::read_mersenne_state("rdm_state_tmp.txt");
+ REQUIRE(writtenA);
+ REQUIRE(writtenB);
+ REQUIRE_THAT(simgrid::xbt::random::uniform_real(10, 20), EpsilonApprox(resB));
+ REQUIRE_THAT(simgrid::xbt::random::normal(0, 2), EpsilonApprox(resC));
+ if (writtenB) {
+ std::remove("rdm_state_tmp.txt");
+ }
+ }
}