shuffle vs random_shuffle in C++


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 function
int randomfunc(int j)
    return rand() % j;
int main()
    vector<int> arr;
    // set some values:
    for (int j = 1; j < 10; ++j)
        // 1 2 3 4 5 6 7 8 9
    // 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 (vector<int>::iterator i = arr.begin(); i != arr.end(); ++i)
        cout << ' ' << *i;
    cout << endl;
    return 0;


arr contains: 5 8 1 7 9 6 4 3 2


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 Program
int 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;



shuffled elements are: 3 1 5 4 2

What is the difference between shuffle and random_shuffle c++?

  1. 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).
  2. shuffle is an improvement over random_shuffle, and we should prefer using the formerfor better results.
  3. Example of Swapping Variables using both
    random 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)]);


    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))]);

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment



load comments

Subscribe to Our Newsletter