How to make sure that std::random_shuffle always produces a different result?
std::random_shuffle
has two forms. One that takes 2 arguments (begin/end iterators), and one that takes 3 (begin/end iterator and a random generator).
The first form uses std::rand()
, so you would use std::srand()
to seed it's random number generator. You can also use the 3-argument version and provide the RNG yourself.
std::random_shuffle
has a template overload for specifying the RNG.
template <class RandomAccessIterator, class RandomNumberGenerator>
void random_shuffle ( RandomAccessIterator first, RandomAccessIterator last,
RandomNumberGenerator& rand );
reference
random_shuffle is deprecated since C++14 (removed in C++17) and replaced with shuffle (exists since C++11) http://en.cppreference.com/w/cpp/algorithm/random_shuffle
possible usage:
shuffle(items.begin(), items.end(), std::default_random_engine(std::random_device()()));