2

Is there anything in the code below that is platform or compiler dependent? It gives different values when compiled with g++ in cygwin than it does when compiled with VC++. I would like to remove any such dependencies if possible.

minstd_rand0 engine;
engine.seed(3113722101);
uniform_int_distribution<int> day_dist(1, 365);
uniform_real_distribution<double> quant_dist(0.0, 1.0);
ofstream fout(argv[1]);
for (int i = 0; i < years; ++i) {
    fout << (i + 1) << "\t";
    fout << day_dist(engine) << "\t";
    fout << quant_dist(engine) << "\t";
    fout << quant_dist(engine) << "\t";
    fout << quant_dist(engine) << "\n";
}
fout.close();
EricAtAIR
  • 73
  • 4

2 Answers2

3

The specifications for engines specify the exact algorithm, so their outputs should be the same on all platforms.

The specifications for distributions specify the result, but not the algorithm. Their outputs can vary from platform to platform, even when they use the same engine.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • This seems to be the likely explanation. It's what I suspected, but it's nice to get confirmation. I know that default_random_engine is implementation dependent, so I removed that from my code and tried the random engines until I found the one that gave me the same values with g++ on my computer. Then I was surprised to find that it was still giving different values with VC++ and suspected that the distributions were also implementation defined (because what else could it even be). – EricAtAIR Dec 17 '18 at 18:22
  • A bit more info: I checked the documentation, but didn't find somewhere where it said "implementation defined": https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution, unlike the case of default_random_engine. Does the expression "satisfies all requirements of..." mean implementation defined (subject to those requirements)? – EricAtAIR Dec 17 '18 at 18:26
  • 1
    @EricAtAIR -- "implementation defined" means that the implementation can choose from various alternatives and that it **must tell you what it does**. The details of distributions are not implementation defined. – Pete Becker Dec 17 '18 at 18:40
2

There are no restrictions on the uniform distributions other than that they produce a uniform distribution.

The standard library authors are allowed to use whatever algorithm and implementation that they like so will likely produce different results on different platforms.

Your option is to produce your own distribution or find an open-source one with an appropriate licence.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60