Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate the same random number sequence over multiple types of compilers and kernels with <random>?

Tags:

People also ask

Can you generate the same random numbers everytime?

random seed() example to generate the same random number every time. If you want to generate the same number every time, you need to pass the same seed value before calling any other random module function. Let's see how to set seed in Python pseudo-random number generator.

What function can be used to force random () to produce the same sequence of numbers each time a program is run?

The rand() function is specifically required to produce the same sequence of numbers when seeded with a given seed (by calling srand() ); each possible seed value specifies a sequence.

What are two methods you can use to generate random numbers?

There are two main methods that a computer generates a random number: true random number generators (TRNGs) and pseudo-random number generators (PRNGs).

How do you generate the same random number in Java?

random() method in the Math class which returns a random floating point number (double) between 0 and 1. To generate random integer numbers between 1 and 30 inclusive: int number = (int) (Math. random() * 30 + 1);


The problem

I need to produce the same (pseudo) random number sequence on different machines and compilers. If I use the same kernel, it seems that the implementetion of mersenne twister (MT) in g++ works well: regardless if I compile my program on a newer machine, with g++ 4.9 or 4.7, I get the same random numbers. But I get different ones if I use older kernel or if I change to Visual Studio's compiler. That's ok, because there's no gurantee that mersenne_twister_engine::seed sets the internal state to the same over different compilers.

What I've already tried

I tought that applying operator<< on the generator produces a unique result that can be used to set the generators on other machines with the operator>>, but in case of mt19937, it seems it is not working. To make it clear, on a computer A I had the code

mt19937 generator1A;
uniform_int_distribution<int> distribution(0, 1000);

cout << "Generating random numbers with seed 1000" << endl;

generator1A.seed(1000);
generator1A(); //to advance the state by one so operator>> will give a longer output; this is not necessary indeed
ofstream test("testseed1000.txt");
test << generator1A << endl;

for (int i = 0; i < 10; ++i)
    cout << distribution(generator1A) << endl;

And it produces 252, 590, 893, ..., and a long file. I transfer the file to the other machine B, and run the following code:

mt19937 generator1B, generator2B;
uniform_int_distribution<int> distribution(0, 1000);

cout << "Generating random numbers with seed 1000, and with operator>>" << endl;
generator2B.seed(1000);
generator2B(); // to advance the state by one here as well

ifstream test("testseed1000.txt");

test >> generator1B;
cout << "************************" << endl;
cout << generator1B << endl;
cout << "************************" << endl;
cout << "With seed\twith operator>>" << endl;

for (int i = 0; i < 10; ++i)
    cout << distribution(generator2B) << "\t" << distribution(generator1B) << endl;

And it produces

654     205
205     115
115     610

The question

Can you give advices how to generate the same (pseudo) random numbers with at least VC++ on Windows and g++ on Debian and Ubuntu? I'd like to use std if it is possible and I wouldn't like to implement my own MT engine.

Notes:

  • creating millions of random numbers and then reading in is not a solution
  • I have to use MSVS for code developing and unix servers for simulation
  • other than MT engines are also welcomed but I prefer MT