1

I am trying to save the state of a random number generator as follows:

std::mt19937 rng
std::ofstream ofile("RngState.txt");
ofile << rng;
ofile.close();

What I observe is the state is a vector of 1248 numbers And only 624 numbers get written in the file. Is there a way to write and read all the 1248 numbers in one attempt(I guess I am trying to increase the capacity/size of ofstream).

Thanks in advance.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
user1612986
  • 1,373
  • 3
  • 22
  • 38
  • Maybe only 624 numbers represent the state. Related to: http://stackoverflow.com/questions/18361050/saving-random-number-generator-state-in-c11 – knivil Mar 14 '16 at 15:36
  • most probably not. all the 1248 number matters. Because when i see the numbers in the debugger i see all of them changing. moreover when i try to read back the state and generate the next random number i observe it is different from random number which i get when i compare with a parallel instance of generator. – user1612986 Mar 14 '16 at 15:42

2 Answers2

1

As @knivil said, state could be represented with only 624 numbers. Please tell us how did you observe 1248?

VS 2015

EDIT:

I have consistent results with this code, could you run it and check too?

#include <fstream>
#include <random>
#include <iostream>

std::mt19937 deser(std::string fname)
{
  std::ifstream f{fname, std::ifstream::binary};
  std::mt19937  res;
  f >> res;
  return res;
}
void ser(std::string fname, std::mt19937 rng)
{
  std::ofstream f(fname, std::ofstream::binary);
  f << rng;
}

void printRand(std::mt19937 rng)
{
  std::uniform_int_distribution<> uid{1, 100};
  std::cout << uid(rng) << std::endl;
}

int main()
{
  auto fname{"R:\\RngState.txt"};

  std::mt19937 rng{std::random_device{}()};
  ser(fname, rng);
  printRand(rng);

  rng = deser(fname);
  printRand(rng);

  return 0;
}
yuyoyuppe
  • 1,582
  • 2
  • 20
  • 33
  • okay i see what you are saying. this is what i did: 1> in one instance of visual studio observe the generator without any saving. 2> in another instance of visual studio write and read. i observe that the vector _Ax which you display is of size 1248 and the numbers from index 624 to 1247 is different between the two instance above. Also I do not get back the same random number that I was expecting to get back. – user1612986 Mar 14 '16 at 15:47
  • @user1612986 not sure if understood you right, but please test my example – yuyoyuppe Mar 14 '16 at 16:08
  • @user1612986 *"Also I do not get back the same random number that I was expecting to get back."* That would be grounds for a new question including a [mcve]. – Baum mit Augen Mar 14 '16 at 16:09
0

As evident from the algorithm, a Mersenne Twister engine needs to remember

n = "degree of occurrence"

integers of size

w = "word size"

bits to generate the series. For mt19937, it holds n = 624 and w = 32 by definition. So the state of mt19937 is uniquely determined by 624 32-bit integers.

Your implementation of the generator may of course safe more temporary results to speed up computation, but the state of the engine is given by exactly those 624 integers, there is no point in writing the rest of the state.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182