Neil Kirk
Neil Kirk

Reputation: 21803

Is std::random_shuffle reproducible across different compilers?

I am using the std::random_shuffle function with a custom random number generator that, for the same seed, returns the same sequence of random numbers across all compilers.

However, I'm concerned std::random_shuffle may not use the same algorithm between different compilers, and therefore, for the same seed, the result won't be the same.

Can I rely on std::random_shuffle producing the same output across different compilers with the same sequence of random numbers provided? If not, any alternatives?

Not using C++11 or Boost.

Upvotes: 3

Views: 282

Answers (1)

Peter - Reinstate Monica
Peter - Reinstate Monica

Reputation: 16057

From reading "25.3.12 Random shuffle" in the C++11 standard (the one I have here) I would conclude that strictly spoken that guarantee can not be made. The only requirement for the algorithm is "that each possible permutation of those elements has equal probability of appearance". It does not have to swap the elements front to back, for example, and the iterators are random access iterators so that any other order is possible. (That said, I'd be surprised if an implementation wouldn't go first -> last, but it's not guaranteed.)

Upvotes: 2

Related Questions