Definition of C++ shuffle()
The shuffle() function in C++ is a function in vector library. It is a function that will rearrange the elements of any range by placing the elements at random positions. To shuffle it uses a uniform random generator which helps in shuffling the elements. It will swap places within the vector and create a newly position vector. The specialty of this function is that we can create our own function for randomly placing the elements. If we do not provide a random generator function the function will have its own random generator. Let us check the syntax, working, and a few examples.
Syntax:
template <class RandomAccessIterator, class URNG>
void shuffle (RandomAccessIterator first, RandomAccessIterator last, URNG&& g);
Let us check what each keyword and parameter signify in this function
- We call the RandomAccessIterator. The first parameter points to the position for the first element in the range which will be rearranged.
- The second parameter points to the last element in the range which will be rearranged. For this also it will be pointing to random access iterator.
- The last parameter g is a special function object which helps us in generating random numbers. It is called a uniform random number generator.
- The return value of this function will be none.
How does C++ shuffle Work?
Using the C++ shuffle function is easy. Let us check how it works.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
void shuf(std::vector<int> const &v)
{
for (int i: vec) {
std::cout << i << ' ';
}
}
int main()
{
std::vector<int> vec = { 1, 27, 38, 42, 50, 69, 72, 87, 99 };
std::shuffle(vec.begin(), vec.end());
shuf(vec);
return 0;
}
We need to import the vector library in order to use the shuffle() function. The user-defined function is displaying the shuffled vectors. In the main function, we have created a vector with a few numbers. The shuffle() function has a beginning and an end which takes the vector elements and shuffles them. Once this is done, we call the function which will print the shuffled array. We have not specified the random generation function hence it will take the default function which can be used. It will rearrange the elements in the vector. The function will swap the value of each element with any other randomly picked element from the same vector. It works with generators that work like the rand() function. To use this function without a generator we can use the random_shuffle(). Let us check a few examples which will help us understand the function better.
Examples of C++ shuffle()
Following are the examples is given below:
Example #1
Code:
#include <iostream>
#include <algorithm>
#include <array>
#include <random>
#include <chrono>
using namespace std;
int main () {
array<int,8> shuf {19,24,37,42,54,76,58,53};
unsigned num = chrono::system_clock::now().time_since_epoch().count();
shuffle (shuf.begin(), shuf.end(), default_random_engine(num));
cout << "The numbers after shuffling are:";
for (int& x: shuf) cout << ' ' << x;
cout << '\n';
return 0;
}

4.5 (8,162 ratings)
View Course
Output:
Code Explanation: The above code is an example of a shuffle function. We have used the iostream library, array library, random and Chrono libraries. Here the Chrono library is used in order to create a random generator. We have taken an array with a size of 8 integers. Here we have defined this array and then we are using the random generator function using the Chrono library. We are generating a random number by using epoch() and now() function which is a part of the clock library. It creates a pattern using which the numbers are shuffled. Then we have called the shuffle function where we define the start and end of the array and the third parameter is the variable that stores the calculation for random number generation. We then print the randomly shuffled array at the end of the program. Below will be the output of the above program.
Example #2
Code:
// C++ program to shuffle an array using the shuffle() method
#include <bits/stdc++.h>
using namespace std;
void edu_shuffle(int arr[], int n)
{
// To create a random formula for shuffling
unsigned rnd = 0;
// Shuffling array using shuffle function
shuffle(arr, arr + n,
default_random_engine(rnd));
// Displayingthe shuffled array
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << endl;
}
// Code which drives the program
int main()
{
int arr[] = { 18, 23, 30, 47, 87, 49};
int num = sizeof(arr) / sizeof(arr[0]);
edu_shuffle(arr, num);
return 0;
}
Output:
Code Explanation: In this program, we have imported a library and created a user-defined function edu_shuffle. This function is first creating an unsigned integer variable that will store the random generation calculation. We then use the shuffle() function where we are passing the start and end of elements between which the shuffling should take place. In the place of random generation, we have used an inbuilt function default_random_engine to create a random number. In the main function, we have calculated the end of the elements which is sent to the edu_shuffle function. We have used the sizeof function. We have sent these as parameters to the user-defined function which helps in executing the shuffle() function. The output of the above function will be as below:
Advantages of C++ shuffle()
The advantages of C++ shuffle function are as below:
- The shuffle function helps in generating a random sequence of numbers easily.
- This function swaps numbers with internal elements quickly.
- If there is no random generator function specified the default of the shuffle() function’s will be taken
- It is fast and efficient which makes it easy to use
- The randomness of numbers can be built and also be used with the C++98/03 standard.
Conclusion
The shuffle() function is an easy way of rearranging the elements of a vector or array. For generating the pattern of random number a random generator variable can be used. It helps in defining the pattern else it uses the default functionality which is provided by the function. It swaps the elements in a given range. This range can be between any elements in the array. This function is similar to random_shuffle() function. The only difference is, shuffle() uses a uniform random number generator.
Recommended Articles
This is a guide to C++ shuffle(). Here we also discuss the definition and how does c++ shuffle work along with examples and its code implementation. You may also have a look at the following articles to learn more –