lundi 8 juin 2020

How to generate the same random number on personal computer and cluster (c++)

I am wondering how to generate EXACTLY the same random numbers using my personal computer and clusters.

Here is a simple code.

#include <iostream>
#include <random>

int main()
{
    int N = 10;

    for (int j = 0; j < N; j++)     std::cout << rand() % 10 << " ";;

}

The output from my personal computer is: 1 7 4 0 9 4 8 8 2 4

The output from cluster is: 3 6 7 5 3 5 6 2 9 1

The difference of these random number will strongly affect my calculations. Moreover, my problem is very flexible and I cannot use the generated random numbers from my personal computer and use it as input on cluster. I hope to generate the same random numbers on different platform.

///////// new try: I tried the solution in the link: If we seed c++11 mt19937 as the same on different machines, will we get the same sequence of random numbers

I used the code:

#include <random>
#include <iostream>

int main()
{
    /* seed the PRNG (MT19937) using a fixed value (in our case, 0) */
    std::mt19937 generator(0);
    std::uniform_int_distribution<int> distribution(1, 10);

    /* generate ten numbers in [1,10] (always the same sequence!) */
    for (size_t i = 0; i < 10; ++i)
    {
        std::cout << distribution(generator) << ' ';
    }
    std::cout << std::endl;

    return 0;

}

On my PC, I got the output: 5 10 4 1 4 10 8 4 8 4

On cluster, I got: 6 6 8 9 7 9 6 9 5 7

Still, it is different.

Can anyone give me an example of the code?

Thanks a lot.




Aucun commentaire:

Enregistrer un commentaire