Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are std::shuffle methods being deprecated in C++14?

According to the cppreference.com reference site on std::shufle, the following method is being deprecated in c++14:

template< class RandomIt > void random_shuffle( RandomIt first, RandomIt last ); 

Why will we no longer be able to call the following function without passing a third parameter?

std::random_shuffle(v.begin(),v.end()); //no longer valid in c++14 

It doesn't appear as though a different function deceleration has a default parameter set. What is the reason behind this? Was there some kind of alternative added?

like image 420
Trevor Hickey Avatar asked Mar 24 '14 02:03

Trevor Hickey


People also ask

How do you shuffle in C++?

The shuffle() function in C++ is a function in vector library. It is a function that will rearrange the elements of any range by placing the elements at random positions. To shuffle it uses a uniform random generator which helps in shuffling the elements.

What is the use of random shuffle function of STL algorithm?

The random_shuffle algorithm shuffles the elements of a sequence (first..last) in a random order. The predicate version uses the pred function to generate the indices of the elements to swap. The pred has to be a function object that takes a parameter n and returns an integral random number in the range 0 to (n - 1).


1 Answers

std::random_shuffle may make use, under the hood, of random C family of functions. These functions use global state for seeds and other state.

So it is being deprecated because shuffle will do the same, but better. Namely, it uses the new <random> header from C++11 that doesn't use global state, but its own objects making use of generators, devices and distributions.

like image 197
Germán Diago Avatar answered Sep 23 '22 01:09

Germán Diago