Home > OS >  Why are the outputs of the same code using Linux and visual studio different?
Why are the outputs of the same code using Linux and visual studio different?

Time:09-21

I compiled exactly the same code in two different environments (Linux and visual studio). But I noticed that the outputs are not the same. I tried to solve this problem in different ways but failed. I need to know why?

Is it something I missed or what?

I want the outputs to be the same to keep progressing my project. Could anyone help please.

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)
            continue;

        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()
{

    string filename = ".//dataset.csv";
    static array<array<int, 20>, 5430> array_X_dataset{};
    static array<int, 5430> array_Y_dataset{};

    bool error = read_data_set(filename, array_X_dataset, array_Y_dataset);
    if (error) {
        printf("Exiting with error while reading dataset file \n");
        exit(-1);
    }
    

    srand(3);
    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"); // the outputs are different 
      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]);
          printf("\n");


      }
}

    

CodePudding user response:

You are using rand() to randomize the output. rand() can use a different pseudo random number generator in different implementations. Hence, using the same seed (srand(3)) is not sufficient to guarantee getting identical results when compiling and running your code for Windows and Linux. See here for more details: Why does the C stdlib rand() function give different values for the same seed across platforms? (thanks to phuclv for the link).

I am not very familar with <random>, maybe it can help here. Though, quoting Mgetz comment:

unfortunately is specified but not specified well enough to be reproducible across toolchains or even versions. That said it's still worlds better than rand()

If that is the case you could use some code to write a sequence of random numbers to a file and then read the numbers from that same file instead of using rand() directly. In this way you can make sure to use the same sequence of random numbers no matter where you run the code. Of course you might sill see different results, but then you know that the reason is not rand() but in your code. First you need to exclude that source of deviation.

However, quoting from Pete Beckers comment:

The generators in are fully specified and required to produce the same sequences on all implementations. The distributions aren't. So if you're just creating raw numbers and managing your own distribution (as the code in the question does with rand() % len, <random> provides exactly the right tool.

CodePudding user response:

The IDEs you use are different. The IDE compiler may work differently.

  • Related