1

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.

Yuan
  • 33
  • 3
  • 3
    related/dupe: https://stackoverflow.com/questions/48730363/if-we-seed-c11-mt19937-as-the-same-on-different-machines-will-we-get-the-same – NathanOliver Jun 08 '20 at 18:32
  • Does this answer your question? [If we seed c++11 mt19937 as the same on different machines, will we get the same sequence of random numbers](https://stackoverflow.com/questions/48730363/if-we-seed-c11-mt19937-as-the-same-on-different-machines-will-we-get-the-same) – Abhishek Bhagate Jun 08 '20 at 18:37
  • 2
    `Still, it is different.` Did you read the answer from the linked question? Just use the generator - distribution can result in different values. – KamilCuk Jun 08 '20 at 18:50
  • Consider using a file? (pre written, and distributed prior to start-up) – 2785528 Jun 08 '20 at 18:52
  • If you don't care much about quality of generated numbers, try `std::mt19937 gen(0); for (...) { auto rand_value = gen() % 10; ... }`. – Evg Jun 08 '20 at 18:57
  • 1
    Thank you so much Evg! Yes, this is exactly what I need. I do not care about the quality of generated numbers. Your code works perfectly! Thank you! – Yuan Jun 08 '20 at 19:18

2 Answers2

0

This question explains that a sequence of numbers you get from

std::uniform_int_distribution<int> distribution(1, 10);

is implementation defined even if you use the same PRNG with the same seed. In contrast, the sequence of random numbers produced by PRNG std::mersenne_twister_engine is well-defined in any standard-conforming implementation.

So, the simplest way to get a well-defined sequence of pseudo-random numbers without using external libraries is this:

#include <iostream>
#include <random>

int main() {
    std::mt19937 generator(0);

    while (true) {
        const auto rand = generator() % 10;
        std::cout << rand << ' ';
    }
}

This code is guaranteed to always produce the same sequence, 4 9 3 0 3 9 7 3 7 3 .... These numbers are not uniformly distributed for the same reason why those produced by rand() % 10 are not. If you don't care much about quality of random numbers, this may be an acceptable solution.

Evg
  • 25,259
  • 5
  • 41
  • 83
-2

Really easily you can use a fixed seed for the random function. Doing so you will always end up with the same sequence on the PC and on the cluster.

#include <iostream>
#include <random>

#define FIXED_SEED 12345 // It can be any integer

int main()
{
    int N = 10;

    srand(FIXED_SEED)

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

}

More over since you're using C++ you can use C++ random machine to genarate pseudo-random sequences. You can obtain the same result with them, but the distribution is more precise, not a big deal anyway.

  • 3
    This is not right. `rand` is implementation defined and can behave completely different on different machines. You need to use a specific engine from the `` header AND roll your own distribution since they are not guaranteed to behave the same on different machines either. – super Jun 08 '20 at 18:50
  • 1
    Thank you Luca. I tried the code. But on my PC, I got: 4 4 5 5 8 5 7 3 2 4, while on cluster, I got 9 1 3 1 1 4 7 2 8 8. Still the different random number. For my research, I hope to generate EXACTLY numbers on different machines. – Yuan Jun 08 '20 at 18:53