Given an array, the task is to shuffle the whole array and print it.
Example
Input (1, 2, 3, 4, 5, 6, 7}
Output {3, 1, 6, 7, 2, 4, 5}
Input (1, 2, 3}
Output {3, 1, 2}
STL contains two methods which can be used to get a shuffled array. These are namely shuffle() and random_shuffle().
shuffle
This method rearranges the elements in the range [first, last) randomly, using g as a uniform random number generator. It swaps the value of each element with that of some other randomly picked element. It determines the element picked by calling g().
Template
template
void shuffle (RandomAccessIterator first,
RandomAccessIterator last,
URNG&& g)
{
for (auto i=(last-first)-1; i>0; --i) {
std::uniform_int_distribution d(0, i);
swap (first[i], first[d(g)]);
}
}
Implementation
// C++ program to shuffle // the given array // using suffle() method #include <bits/stdc++.h> using namespace std; // Shuffle array void shuffle_array(int arr[], int n) { // To obtain a time-based seed unsigned seed = 0; // Shuffling our array shuffle(arr, arr + n, default_random_engine(seed)); // Printing our array for (int i = 0; i < n; ++i) cout << arr[i] << " "; cout << endl; } // Driver code int main() { int a[] = { 10, 20, 30, 40 }; int n = sizeof(a) / sizeof(a[0]); shuffle_array(a, n); return 0; } |
30 10 20 40
Note: Output may differ each time because of the random function used in the program.
random_shuffle
This function randomly rearranges elements in the range [first, last). It swaps the value of each element with some other randomly picked element. When provided, the function gen determines which element is picked in every case. Otherwise, the function uses some unspecified source of randomness.
Template
template
void random_shuffle (RandomAccessIterator first,
RandomAccessIterator last,
RandomNumberGenerator& gen)
{
iterator_traits::difference_type i, n;
n = (last-first);
for (i=n-1; i>0; --i) {
swap (first[i], first[gen(i+1)]);
}
}
Implementation
// C++ program to shuffle // the given array // using random_suffle() method #include <bits/stdc++.h> using namespace std; // Shuffle array void shuffle_array(int arr[], int n) { // To obtain a time-based seed unsigned seed = 0; // Shuffling our array using random_shuffle random_shuffle(arr, arr + n); // Printing our array for (int i = 0; i < n; ++i) cout << arr[i] << " "; cout << endl; } // Driver code int main() { int a[] = { 10, 20, 30, 40 }; int n = sizeof(a) / sizeof(a[0]); shuffle_array(a, n); return 0; } |
10 40 20 30
Note: Output may differ each time because of the random function used in the program.
Which is better?
- shuffle introduced after C11++, uses functions which are better than rand() which random_shuffle uses.
- shuffle is an improvement over random_shuffle, and we should prefer using the former for better results.
- If we don’t pass our random generating function in random_shuffle then it uses its unspecified random values due to which all successive values are correlated.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- shuffle vs random_shuffle in C++
- Check if possible to shuffle a matrix with adjacent movements
- Sorting an array according to another array using pair in STL
- Count number of unique Triangles using STL | Set 1 (Using set)
- Sorting Array Elements By Frequency | Set 3 (Using STL)
- Count the number of 1's and 0's in a binary array using STL in C++ ?
- Array product in C++ using STL
- Finding Floor and Ceil of a Sorted Array using C++ STL
- Sort a nearly sorted array using STL
- Finding Median of unsorted Array in linear time using C++ STL
- Working with Array and Vectors using STL in C++
- How to find the sum of elements of an Array using STL in C++?
- How to sort an Array in descending order using STL in C++?
- How to reverse an Array using STL in C++?
- How to find the maximum element of an Array using STL in C++?
- How to sort an Array using STL in C++?
- How to find the minimum and maximum element of an Array using STL in C++?
- Program to check if an Array is Palindrome or not using STL in C++
- All permutations of an array using STL in C++
- Elements present in first array and not in second using STL in C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

