-1

I've a code when I run in visual studio, I get different results and when I run using g++ compiler I get different results. It has a seed of 1, so I guess this should not affect it, Also there are some parts of the code which run in thread(but this part doesn't contain any rand function)

I get the same results by running the application on the same platform , but different if I use different compilers

user3345850
  • 141
  • 2
  • 11
  • Maybe http://stackoverflow.com/questions/3958795/different-rand-results-on-windows-and-linux ? – greedy52 Feb 06 '17 at 14:16
  • 1
    If you have undefined behavior you can get what you described. You can also get different results in different runs with the same compiler. – NathanOliver Feb 06 '17 at 14:17
  • [Look here](http://stackoverflow.com/questions/7209094/why-can-different-c-compilers-give-different-outputs-for-a-c-program). It depends on your code so it's hard to tell without your code/ an example. – izlin Feb 06 '17 at 14:17
  • What code? By seed are you referring to a PRNG? – Lightness Races in Orbit Feb 06 '17 at 14:55

3 Answers3

5

For all behaviour that the standard defines, programs generated by all compilers must behave the same way.

For all behaviour that the standard leaves unspecified, compilers do not need to behave the same. The standard makes no guarantees about programs that violate the standard for example. The standard also leaves many details up to the implementation.

Also, compilers tend to not always comply to the standard in all cases and some compilers may not support same version of the standard as another.

Finally, some standard rules are found to be ambiguous, and different compilers may have chosen an opposite interpretation. These should be documented as defect reports.

.. seed ... rand function ...

The random sequence produced by rand is implementation defined. Yes, the results can be different with different compilers.


C++11 introduced <random> header. Of the random number generators defined there, default_random_engine is the only one that has implementation defined behaviour.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • _"For all behaviour that the standard defines, all compilers must behave the same way"_ While correct, that does not mean that they always do. Indeed, quite a lot of questions on SO identify programs that different compilers treat differently, either due to extensions or outright bugs. Not to mention different standard versions. – Lightness Races in Orbit Feb 06 '17 at 14:54
  • @LightnessRacesinOrbit I've edited the caveats into the question. Most language extensions benefit from the fact that the standard doesn't specify any behaviour for ill-formed programs. – eerorika Feb 06 '17 at 15:07
  • That's actually not true, unless the phrase "no diagnostic required" is included after the declaration of ill-formity. Otherwise a diagnostic is required. – Lightness Races in Orbit Feb 06 '17 at 15:34
  • @LightnessRacesinOrbit diagnostic is part of the behaviour of the compiler, not the program. (This is why I edited the part of my answer that you quoted). For example, a compiler that supports VLA can be fully standard conforming, as long as a diagnostic message is shown when compiling a program that uses VLA. – eerorika Feb 06 '17 at 16:02
  • The standard mandates diagnostics for ill-formed programs unless otherwise specified. Fair enough that's not really program behaviour. – Lightness Races in Orbit Feb 06 '17 at 16:11
  • @LightnessRacesinOrbit That is correct. And the standard explicitly allows implementation to compile and execute such programs, while still being conforming. – eerorika Feb 06 '17 at 16:16
3

If you want a reproducible pseudo-random numbers, use the C++ facilities instead, so you can choose a well-defined generator.

The C pseudo-random-number generator isn't guaranteed to be the same across compilers or platforms.

Useless
  • 64,155
  • 6
  • 88
  • 132
-1

Pseudo-random number generation is a very tough problem. It is better to use a third party library. For example, there are several random number generators included with the GNU Scientific Library (https://www.gnu.org/software/gsl/). When I need a reproducible random number generator I tend to use the Mersenne Twister which is the GSL default.

Random number generation for cryptography is done with other libraries.

Also you can test the quailty of you generator is using Die Harder II (https://www.phy.duke.edu/~rgb/General/dieharder.php). And yes, technically you cannot tell how random something is, but if you can find a pattern in it - it is definitely not random.

Robert Baron
  • 214
  • 1
  • 9
  • 1
    You don't need a third-party library to get reproducible random numbers. C++ has had a powerful random number generation library since C++11. – Pete Becker Feb 06 '17 at 14:55