shuffle vs random_shuffle in C++

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 function
int 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 (vector<int>::iterator 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 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;
}

Output:

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

    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.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.