shuffle vs random_shuffle in C++ - GeeksforGeeks

09 Oct, 2019
random_shuffle
It randomly rearrange elements in range [first, last).The function swaps the value of each element with some other randomly picked element. When provided, the function gen determines which element is picked in every case. Otherwise, the function uses some unspecified source of randomness.
// CPP program Illustrating the// use of random_shuffle#include <bits/stdc++.h>using namespace std;  // random generator functionint randomfunc(int j){    return rand() % j;}  int main(){    srand(unsigned(time(0)));    vector<int> arr;      // set some values:    for (int j = 1; j < 10; ++j)          // 1 2 3 4 5 6 7 8 9        arr.push_back(j);      // using built-in random generator    random_shuffle(arr.begin(), arr.end());      // using randomfunc    random_shuffle(arr.begin(), arr.end(), randomfunc);      // print out content:    cout << "arr contains:";    for (auto i = arr.begin(); i != arr.end(); ++i)        cout << ' ' << *i;      cout << endl;      return 0;}
Output:
arr contains: 5 8 1 7 9 6 4 3 2

shuffle
Rearranges the elements in the range [first, last) randomly, using g as uniform random number generator.The function swaps the value of each element with that of some other randomly picked element. The function determines the element picked by calling g().
// CPP program Illustrating// the use of shuffle#include <bits/stdc++.h>using namespace std;  // Driver Programint main(){    array<int, 5> s{ 1, 2, 3, 4, 5 };      // To obtain a time-based seed    unsigned seed = 0;      // Use of shuffle    shuffle(s.begin(), s.end(), default_random_engine(seed));      cout << "shuffled elements are:";    for (int& i : s)        cout << ' ' << i;    cout << endl;      return 0;}
Output:
 
shuffled elements are: 3 1 5 4 2

What is the difference between shuffle and random_shuffle c++?
The only difference is that random_shuffle uses rand() function to randomize the items, while the shuffle uses urng which is a better random generator, though with the particular overload of random_shuffle, we can get the same behavior (as with the shuffle).shuffle is an improvement over random_shuffle, and we should prefer using the formerfor better results.Example of Swapping Variables using bothrandom shuffle:template (class RandomIt, class RandomFunc)
void random_shuffle(RandomIt first, RandomIt last, RandomFunc&& r)
{
    typename iterator_traits::difference_type i, n;
    n = last - first;
    for (i = n-1; i > 0; --i) {
        using std::swap;
        swap(first[i], first[r(i+1)]);
    }
}
Shuffle:template void shuffle(RandomIt first, RandomIt last, 
             UniformRandomBitGenerator&& g)
{
    typedef typename iterator_traits::difference_type diff_t;
    typedef uniform_int_distribution distr_t;
    typedef typename distr_t::param_type param_t;
 
    distr_t D;
    diff_t n = last - first;
    for (diff_t i = n-1; i > 0; --i) {
        using swap;
        swap(first[i], first[D(g, param_t(0, i))]);
    }
}
The only difference is that random_shuffle uses rand() function to randomize the items, while the shuffle uses urng which is a better random generator, though with the particular overload of random_shuffle, we can get the same behavior (as with the shuffle).
shuffle is an improvement over random_shuffle, and we should prefer using the formerfor better results.
Example of Swapping Variables using bothrandom shuffle:template (class RandomIt, class RandomFunc)
void random_shuffle(RandomIt first, RandomIt last, RandomFunc&& r)
{
    typename iterator_traits::difference_type i, n;
    n = last - first;
    for (i = n-1; i > 0; --i) {
        using std::swap;
        swap(first[i], first[r(i+1)]);
    }
}
Shuffle:template void shuffle(RandomIt first, RandomIt last, 
             UniformRandomBitGenerator&& g)
{
    typedef typename iterator_traits::difference_type diff_t;
    typedef uniform_int_distribution distr_t;
    typedef typename distr_t::param_type param_t;
 
    distr_t D;
    diff_t n = last - first;
    for (diff_t i = n-1; i > 0; --i) {
        using swap;
        swap(first[i], first[D(g, param_t(0, i))]);
    }
}
template (class RandomIt, class RandomFunc)
void random_shuffle(RandomIt first, RandomIt last, RandomFunc&& r)
{
    typename iterator_traits::difference_type i, n;
    n = last - first;
    for (i = n-1; i > 0; --i) {
        using std::swap;
        swap(first[i], first[r(i+1)]);
    }
}

Shuffle:
template void shuffle(RandomIt first, RandomIt last, 
             UniformRandomBitGenerator&& g)
{
    typedef typename iterator_traits::difference_type diff_t;
    typedef uniform_int_distribution distr_t;
    typedef typename distr_t::param_type param_t;
 
    distr_t D;
    diff_t n = last - first;
    for (diff_t i = n-1; i > 0; --i) {
        using swap;
        swap(first[i], first[D(g, param_t(0, i))]);
    }
}
This article is contributed by Shambhavi Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
saurabhgajjar
cpp-algorithm-library
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Inheritance in C++
C++ Classes and Objects
Operator Overloading in C++
Socket Programming in C/C++
Bitwise Operators in C/C++
Multidimensional Arrays in C / C++
Virtual Function in C++
Constructors in C++
Templates in C++ with Examples
Copy Constructor in C++