vendredi 18 août 2017

Platform dependent state of mt19937 in C++?

I want to save the state of the std::mt19937 random number generator in a C++ program, so that I am able to resume my program at a later stage at the same "randomness state". I also want to use my program on different platforms (Linux and Mac).

Consider the following minimal example where I simply write the current state to stdout:

#include <iostream>
#include <random>
using namespace std;

static mt19937 rng;

int main() {
  seed_seq seeder{1234};
  rng = mt19937(seeder);

  cout << "mt1: " << rng() << endl;
  cout << "mt2: " << rng() << endl;

  cout.imbue(locale("en_US.UTF-8"));
  cout << rng << endl;
}

My problem is that this generates different outputs for the state, depending on the platform I compile this. On a Linux system (g++ 7.1.0) I get:

mt1: 2684129121
mt2: 3957864051
3,598,990,873 2,041,003,246 [...]

while on my Mac (Apple LLVM 8.1.0) I get:

mt1: 2684129121
mt2: 3957864051 
1,413,537,266 1,230,536,264 [...]

Basically I want to understand why the states are different and how I can achieve that they are the same so that I can save and load the state between systems.

This question is related to this one: C++ std::mt19937 and rng state save/load & portability However, the thread does not answer my question. It gave the hint of using the same locale, but that does not seem to affect the state.




Aucun commentaire:

Enregistrer un commentaire