Since C++11, std::shuffle()
takes an rvalue reference to a random bit generator:
template<class RandomIt, class URBG>
void shuffle(RandomIt first, RandomIt last, URBG&& g);
And so I might call it thus:
std::vector<int> v = {...};
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(v.begin(), v.end(), g);
This reveals an error in my understanding of C++ that I haven't been able to satisfy through reading this morning: what is gained by using an rvalue reference here? In other words, why is this not
template<class RandomIt, class URBG>
void shuffle(RandomIt first, RandomIt last, URBG& g);