Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
simplify the MC initialization by using only one env variable
[simgrid.git] / src / xbt / random.cpp
index 004418eadd9148ef69acec1505c6c2d0b68d8f02..8439deaff3744d880c17ab8df9ec568c6e5bf50d 100644 (file)
@@ -19,28 +19,24 @@ namespace simgrid {
 namespace xbt {
 namespace random {
 
-bool Random::read_state(std::string filename)
+bool Random::read_state(const 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;
-  }
+  file >> mt19937_gen;
+  file.close();
+  if (file.fail())
+    XBT_WARN("Could not save the RNG state to file %s.", filename.c_str());
+  return not file.fail();
 }
 
-bool Random::write_state(std::string filename)
+bool Random::write_state(const 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;
-  }
+  file << mt19937_gen;
+  file.close();
+  if (file.fail())
+    XBT_WARN("Could not read the RNG state from file %s.", filename.c_str());
+  return not file.fail();
 }
 
 int StdRandom::uniform_int(int min, int max)
@@ -123,12 +119,12 @@ void set_mersenne_seed(int seed)
   default_random->set_seed(seed);
 }
 
-bool read_mersenne_state(std::string filename)
+bool read_mersenne_state(const std::string& filename)
 {
   return default_random->read_state(filename);
 }
 
-bool write_mersenne_state(std::string filename)
+bool write_mersenne_state(const std::string& filename)
 {
   return default_random->write_state(filename);
 }