Click here to Skip to main content
15,392,636 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I compiled exactly the same code that generate random numbers in two different environments ( Linux and visual studio ). But I noticed that the outputs are different. I searched online and understand that the two implementations generate different random numbers. But I need the Linux to generate the same random numbers of that generated by visual studio.

So, how to let the two different environments ( Linux and visual studio ) generate the same random numbers. Any ideas.

My code:

void mix_dataset(array<array<int, 20>, 5430>& array_X_dataset, array<int, 5430>& array_Y_dataset) {
   size_t len = array_X_dataset.size();
    for (size_t i = 0; i < len; ++i) {
        size_t swap_index = rand() % len;  
        if (i == swap_index)

        array<int, 20> data_point{  };
        data_point = array_X_dataset[i];
        array_X_dataset[i] = array_X_dataset[swap_index];
        array_X_dataset[swap_index] = data_point;
        int Y = array_Y_dataset[i];
        array_Y_dataset[i] = array_Y_dataset[swap_index];
        array_Y_dataset[swap_index] = Y;

int main(){
    mix_dataset(array_X_dataset, array_Y_dataset);

    int* array_Y_set = new int[5430];
    int** array_X_set = new int* [5430];
    for (int i = 0; i < 5430; i++) {
        array_X_set[i] = new int[20];
    for (int i = 0; i < 5430; i++) {
        for (int j = 0; j < 20; j++)
            array_X_set[i][j] = array_X_dataset[i][j];
        array_Y_set[i] = array_Y_dataset[i];
      printf("printout the whole dataset after random mixing:\n");
      for (int i = 0; i < 5430; i++) {
          printf(" %d ", i);
          for (int j = 0; j < 20; j++)
              printf(" %d ", array_X_set[i][j]);
          printf(" %d ", array_Y_set[i]);


What I have tried:

I tried some solutions but failed
Updated 17-Sep-21 5:16am
Member 15329613 17-Sep-21 10:32am
You don't want random numbers.
prother123 17-Sep-21 10:34am
The outputs are different because the generated random numbers are different in the two environments. So, generating the same random numbers should give me the same outputs.
Member 15329613 17-Sep-21 10:37am
By they are random and should not be counted on to be the same.

I doubt there is anyway to do what you want, but the question is why do you want to do this? You probably need to generate the numbers some different way and then store them.
prother123 17-Sep-21 10:50am
okay, lets see your suggestion on the code
Member 15329613 17-Sep-21 12:01pm
I still do not know why you want to do it.

As you have discovered, windows and linux have different PRNG algorithms. If you need to have the same "random" numbers generated on any system you compile your code on, you will need to include the PRNG as part of the code. There are various algorithms to choose from, and googling for "psuedo random number algorithm" should provide you with some direction.
Alternatively, as Member 15329613 has suggested, you could pre-generate a number of "random" numbers, save them to a file and then read them in as needed. If you have a need to have different starting points, you could generate far more PRNs than you need per run, and then your equivalent of srand() could skip to some point in the file and start reading from there, cycling back to the beginning of the file when and EOF is detected.
Obviously, these aren't really random numbers, but either technique may produce results that are sufficient for your needs.
The only way to be sure (and use "random numbers" in bulk) is to include your own Pseudorandom number generator - Wikipedia[^] in your code.
Google will help you find example code: prng c - Google Search[^]
One can not rely on various implementations of rand to give the same results. You might have better luck with the STL implementation of a pseudo-random number generator. Here is a snippet of the code I have used to do this.
// initialize the PRNG

std::random_device rd;          // used to obtain a seed
std::mt19937_64 gen( rd() );    // mersenne_twister_engine
std::uniform_int_distribution< INT64 > rnd( 0, highestValue );
INT64 randomvalue = rnd( gen );
There are various types of generators and distributions available.

One other thing - you have used the magic number 5430 several places in your code. That is not recommend practice. It should be a constant value defined in ONE place and passed into various functions. It (the number 5430) should appear only once and that is in the definition of the constant value and that value should be given a descriptive name.
prother123 17-Sep-21 12:31pm
Thank you for reply, so how to modify my
code to include your subcode in??
Rick York 17-Sep-21 14:08pm
Where you call srand, instead use the first three lines to initialize the RNG. The last line shows how to call it. The function rand has a maximum return value of RAND_MAX. With this code it has a maximum of highestValue so if you can still use the modulus of the length to get the RV and you can use almost whatever you want for highestValue - RAND_MAX if you want.
k5054 17-Sep-21 15:37pm
It looks like std::uniform_int_distribution produces different results on windows and linux. Windows and linux both produce the same output for the following program:

int main()
    std::mt19937 rnd(1);

    for(int i = 0; i < 10; ++i)
        std::cout << rnd() << '\n';

I get the same results, namely:
Modifying the program to
#include <cstdint>

int main()
    std::mt19937 gen(1);
    std::uniform_int_distribution<int64_t> rnd(0, 100);

    for(int i = 0; i < 10; ++i)
        std::cout << "rand = " << rnd(gen) << '\n';

I get the following:
Linux   Windows
  42      23
  100     25
  72      37
  94      100
  0       100
  12      95
  30      72
  100     14
  14      49
  23      84
Unless I've done something wrong, here.
Rick York 17-Sep-21 19:44pm
Thanks for trying that. I don't have access to a Linux box so that was helpful.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900