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

shuffle()

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).

The syntax of shuffle() is,

shuffle(iterator start, iterator end, generator function)

Here, iterator start and end defines the range.

C++ code:

#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(),

1) 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

2) 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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.