×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

Shuffling an array using C++ STL function

In this article, we are going to see how to shuffle an array using standard library function shuffle() and random_shuffle() in C++?
Submitted by Radib Kar, on July 14, 2020

Prerequisite: Fisher-Yates algorithm

C++ shuffle() function

shuffle() is a standard library function that comes under the header file algorithm and helps to shuffle the mentioned range of the array randomly using a generator.

Its internal working is exactly similar to the Fisher-Yates algorithm.

The only added thing is this function takes a range to shuffle not the entire array. So, the user can pass the required range to the shuffle() function. Also, except using rand() to create the random index for swapping, we use the generator function here to select randomly the element to be swapped with the end element (ith element).

Syntax

The syntax of shuffle() is,

shuffle(iterator start, iterator end, generator function)

Here, iterator start and end defines the range.

Example

#include <bits/stdc++.h>
using namespace std;

int main()
{
    cout << "input array size\n";
    int n;
    cin >> n;

    cout << "Input array elements \n";

    vector<int> arr(n);
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }

    //taking the entire range
    //iterator start=arr.begin()
    //iterator end=arr.end()
    //generator function=default_random_engine(0)
    shuffle(arr.begin(), arr.end(), default_random_engine(0));

    cout << "After reshuffling, printing the array\n";

    for (auto it : arr)
        cout << it << " ";
    cout << endl;

    return 0;
}

Output

input array size
6
Input array elements
12 43 52 89 5 11
After reshuffling, printing the array
52 12 5 11 43 89

random_shuffling()

random_shuffling() serves the same purpose.

shuffling() was introduced in C++11, earlier there was random_shuffling only. random_shuffling() by default takes rand() as a generator function which causes unspecified randomness. That's why shuffling was introduced in the later version where the generator function performs better than rand(). Though, by overloading we can bring the same performance like shuffling through random_shuffling() too passing user-defined generator function. Below is the example of random_shuffling().

The syntax for random_shuffling is the same exactly as shuffling(),

Without any generator function, rand() by default

#include <bits/stdc++.h>
using namespace std;

int main()
{
    cout << "input array size\n";
    int n;
    cin >> n;

    cout << "Input array elements \n";

    vector<int> arr(n);
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }

    //taking the entire range
    //iterator start=arr.begin()
    //iterator end=arr.end()
    //generator function=rand() by default
    random_shuffle(arr.begin(), arr.end());
    cout << "After reshuffling, printing the array\n";

    for (auto it : arr)
        cout << it << " ";
    cout << endl;

    return 0;
}

Output

input array size
6
Input array elements
56 32 45 82 98 13
After reshuffling, printing the array
98 82 32 45 56 13

User defined generator

#include <bits/stdc++.h>
using namespace std;

int myrandom(int i)
{
    return rand() % i;
}

int main()
{
    cout << "input array size\n";
    int n;
    cin >> n;

    cout << "Input array elements \n";

    vector<int> arr(n);
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }

    //taking the entire range
    //iterator start=arr.begin()
    //iterator end=arr.end()
    //generator function=rand() by default
    random_shuffle(arr.begin(), arr.end(), myrandom);
    cout << "After reshuffling, printing the array\n";

    for (auto it : arr)
        cout << it << " ";
    cout << endl;

    return 0;
}

Output

input array size
6
Input array elements
32 67 3 8 12 6
After reshuffling, printing the array
12 8 67 3 32 6

On a single note, shuffling is better than random_shuffling since you can use generator functions.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.