Updated June 30, 2023
Definition of C++ shuffle()
The shuffle() function in C++ is a function in the vector library. It is a function that will rearrange any range’s elements by placing them at random positions. To shuffle, it uses a uniform random generator which helps mix the elements. It will swap places within the vector and create a new position vector. The specialty of this function is that we can create our function for randomly placing the elements. If we do not provide a random generator function, the function will have its 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 of 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 that helps us generate 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 must import the vector library to use the shuffle() function. The user-defined function displays the shuffled vectors. We have created a vector with a few numbers in the main function. 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 to 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 to help us understand the function better.
Examples of C++ shuffle()
Following are the examples 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;
}
Output:
Code Explanation: The above code is an example of a shuffle function. We have used the iostream, array, random, and Chrono libraries. Here the Chrono library is used 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 using the 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 is 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 first creates an unsigned integer variable that will store the random generation calculation. We then use the shuffle() function, passing the start and end of elements between which the shuffling should occur. In place of random generation, we have used an inbuilt function default_random_engine to create a random number. In the main function, we calculated the end of the elements 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 execute the shuffle() function. The output of the above function will be as below:
Advantages of C++ shuffle()
The advantages of the 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 no random generator function is specified, the shuffle() function default will be taken.
- It is fast and efficient, which makes it easy to use
- The randomness of numbers can be built and used with the C++98/03 standard.
Conclusion
The shuffle() function is an easy way of rearranging the elements of a vector or array. A random generator variable can be used to generate the pattern of random numbers. The library plays a role in defining the “else” pattern by utilizing the default functionality provided by the function. It actively swaps the elements within a given range. This range can be between any elements in the array. This function is similar to the random_shuffle() function. The only difference is shuffle() uses a uniform random number generator.
Recommended Articles
We hope that this EDUCBA information on “C++ shuffle()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.