Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
It is now possible to read/write the RNG state in a file.
authorYann Duplouy <yann.duplouy@inria.fr>
Wed, 15 Apr 2020 09:56:15 +0000 (11:56 +0200)
committerYann Duplouy <yann.duplouy@inria.fr>
Wed, 15 Apr 2020 09:56:15 +0000 (11:56 +0200)
include/xbt/random.hpp
src/xbt/random.cpp

index 26492e2..83c8cbf 100644 (file)
@@ -7,7 +7,10 @@
 #define SIMGRID_XBT_RANDOM_HPP
 
 #include "xbt/base.h"
 #define SIMGRID_XBT_RANDOM_HPP
 
 #include "xbt/base.h"
+#include <fstream>
+#include <iostream>
 #include <random>
 #include <random>
+#include <string>
 
 namespace simgrid {
 namespace xbt {
 
 namespace simgrid {
 namespace xbt {
@@ -34,6 +37,24 @@ public:
    */
   void set_seed(int seed) { mt19937_gen.seed(seed); }
 
    */
   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)
    *
   /**
    * @brief Draws an integer number uniformly in range [min, max] (min and max included)
    *
@@ -111,6 +132,16 @@ void set_implem_std();
  */
 void set_mersenne_seed(int);
 
  */
 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)
  *
 /**
  * @brief Draws an integer number uniformly in range [min, max] (min and max included)
  *
index dd4b3db..3b2962f 100644 (file)
@@ -7,6 +7,7 @@
 #include "xbt/asserts.h"
 #include <limits>
 #include <memory>
 #include "xbt/asserts.h"
 #include <limits>
 #include <memory>
+#include <string>
 
 namespace simgrid {
 namespace xbt {
 
 namespace simgrid {
 namespace xbt {
@@ -92,6 +93,16 @@ void set_mersenne_seed(int seed)
   default_random->set_seed(seed);
 }
 
   default_random->set_seed(seed);
 }
 
+void read_mersenne_state(std::string filename)
+{
+  default_random->read_state(filename);
+}
+
+void write_mersenne_state(std::string filename)
+{
+  default_random->write_state(filename);
+}
+
 int uniform_int(int min, int max)
 {
   return default_random->uniform_int(min, max);
 int uniform_int(int min, int max)
 {
   return default_random->uniform_int(min, max);