Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
It is now possible to read/write the RNG state in a file.
[simgrid.git] / include / xbt / random.hpp
index 8175c69..83c8cbf 100644 (file)
@@ -7,7 +7,10 @@
 #define SIMGRID_XBT_RANDOM_HPP
 
 #include "xbt/base.h"
+#include <fstream>
+#include <iostream>
 #include <random>
+#include <string>
 
 namespace simgrid {
 namespace xbt {
@@ -25,7 +28,7 @@ public:
   /** @brief Build a new random number generator with default seed */
   Random() = default;
   /** @brief Build a new random number generator with given seed */
-  Random(int seed) : mt19937_gen(seed) {}
+  explicit Random(int seed) : mt19937_gen(seed) {}
 
   virtual ~Random() = default;
 
@@ -34,6 +37,24 @@ public:
    */
   void set_seed(int seed) { mt19937_gen.seed(seed); }
 
+  /**
+   * @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;
+  }
+
+  /**
+   * @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;
+  }
+
   /**
    * @brief Draws an integer number uniformly in range [min, max] (min and max included)
    *
@@ -73,7 +94,7 @@ public:
 class XBT_PUBLIC StdRandom : public Random {
 public:
   StdRandom() = default;
-  StdRandom(int seed) : Random(seed) {}
+  explicit StdRandom(int seed) : Random(seed) {}
 
   int uniform_int(int min, int max) override;
   double uniform_real(double min, double max) override;
@@ -88,7 +109,7 @@ public:
 class XBT_PUBLIC XbtRandom : public Random {
 public:
   XbtRandom() = default;
-  XbtRandom(int seed) : Random(seed) {}
+  explicit XbtRandom(int seed) : Random(seed) {}
 
   int uniform_int(int min, int max) override;
   double uniform_real(double min, double max) override;
@@ -111,6 +132,16 @@ void set_implem_std();
  */
 void set_mersenne_seed(int);
 
+/**
+ * @brief Read the state of the Mersenne-Twister RNG from a file
+ */
+void read_mersenne_state(std::string filename);
+
+/**
+ * @brief Write the state of the Mersenne-Twister RNG to a file
+ */
+void write_mersenne_state(std::string filename);
+
 /**
  * @brief Draws an integer number uniformly in range [min, max] (min and max included)
  *