/**
* @brief Read the state of the Mersenne-Twister RNG from a file
*/
- void read_state(std::string filename)
- {
- std::ifstream file(filename);
- file >> mt19937_gen;
- }
+ bool read_state(std::string filename);
/**
* @brief Write the state of the Mersenne-Twister RNG to a file
*/
- void write_state(std::string filename)
- {
- std::ofstream file(filename);
- file << mt19937_gen;
- }
+ 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
+ * @brief Read the state of the Mersenne-Twister RNG from a file.
*/
-void read_mersenne_state(std::string filename);
+bool read_mersenne_state(std::string filename);
/**
- * @brief Write the state of the Mersenne-Twister RNG to a file
+ * @brief Write the state of the Mersenne-Twister RNG to a file.
*/
-void write_mersenne_state(std::string filename);
+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 false;
+ } 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);
}
-void read_mersenne_state(std::string filename)
+bool read_mersenne_state(std::string filename)
{
- default_random->read_state(filename);
+ return default_random->read_state(filename);
}
-void write_mersenne_state(std::string filename)
+bool write_mersenne_state(std::string filename)
{
- default_random->write_state(filename);
+ return default_random->write_state(filename);
}
int uniform_int(int min, int max)