How to Shuffle a Vector in C++?
Last Updated :
26 Nov, 2024
Shuffling a vector means rearranging the position of its elements in random order. In this article, we will learn how to shuffle the vector in C++.
The most efficient way to shuffle a vector is by using shuffle() method. Let’s take a look at an example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5, 6, 7};
// Initialize random number generator
random_device rd;
mt19937 g(rd());
// Shuffle the vector
shuffle(v.begin(), v.end(), g);
for (auto i : v)
cout << i << " ";
return 0;
}
Explanation: The shuffle() function uses random number generator to determine new positions for the elements in the range. Here, mt19937 generator is used.
There are also some other methods in C++ to shuffle a vector. Some of them are as follows:
Using random_shuffle()
The random_shuffle() function is similar to above method, but the random number generator of this function is not efficient as based on rand() function.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5, 6, 7};
// Shuffle the vector
random_shuffle(v.begin(), v.end());
for (auto i : v)
cout << i << " ";
return 0;
}
Using Fisher-Yates Shuffle Algorithm
The Fisher-Yates shuffle algorithm can also shuffle the vector manually by iterating from last to first element in vector and using a random number generator to select elements to be swapped.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5, 6, 7};
// Shuffle the vector
for (int i = v.size() - 1; i >= 0; i--) {
// Generate the random index
int j = rand() % (i + 1);
swap(v[i], v[j]);
}
for (auto i : v)
cout << i << " ";
return 0;
}
Explanation: In this method, we are iterating the vector from last to first element and generate the random index at every index of vector and swap the current index element with the generated random index element.
Using Permutation
In this method, we randomly generate the permutation of vector using functions like next_permutation() until a random one is found. This method is not efficient for large vectors due to factorial growth in permutations.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5, 6, 7};
// Generate the permutation of vector
do {
// Shuffle the vector
random_shuffle(v.begin(), v.end());
} while (!next_permutation(v.begin(), v.end()));
for (auto i : v)
cout << i << " ";
return 0;
}
Similar Reads
How to Resize a 2D Vector in C++? In C++, resizing the 2D vector means increasing or decreasing the row and column size of 2D vector. In this article, we will learn different methods to resize a 2D vector in C++.The most efficient way to resize the 2D vector is by using vector resize() function. Letâs take a look at a simple example
2 min read
How to Iterate 2D Vector in C++? Iterating or traversing a 2D vector means accessing each element of the 2D vector sequentially. In this article, we will explore different methods to iterate over a 2D vector in C++.The simplest way to iterate a 2D vector is by using a range-based for loop. Let's take a look at a simple example that
3 min read
How to Initialize 2D Vector in C++? Initializing a 2D vector refers to the process of assigning initial values to the elements of a 2D vector. In this article, we will learn different methods to initialize a 2D vector in C++.The simplest way to initialize a 2D vector is by passing the elements inside an initializer list. This method i
3 min read
How to Create a Set of Vectors in C++? In C++, a set is a type of associative container in which duplicate elements are not allowed and a vector is a dynamic array in which duplicate elements are allowed. In this article, we will learn how to create a set of vectors in C++. For Example, Input:vector<int> vec1={1, 2, 3};vector<in
2 min read
How to Reverse Iterate a Vector in C++? In this article, we will learn different methods to iterate through the vector in reverse order in C++.The most efficient method to iterate through the vector in reverse order is by using reverse iterator. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int mai
2 min read