Shuffle an Array using STL in C++
Last Updated :
27 Dec, 2020
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
CPP
// C++ program to shuffle
// the given array
// using shuffle() 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;
}
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
CPP14
// C++ program to shuffle
// the given array
// using random_shuffle() 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;
}
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.
Similar Reads
How to Reverse an Array using STL in C++? Reversing an array means rearranging its elements so that the first element becomes the last, the second element becomes the second last, and so on. In this article, we will learn how to reverse an array using STL in C++.The most efficient way to reverse an array using STL is by using reverse() func
2 min read
Find All Permutations of an Array using STL in C++ The permutation of an array refers to a rearrangement of its elements in every possible order. In this article, we will learn how to generate all possible permutation of an array using STL in C++.The simplest method to find all the permutations of an array is to use next_permutation(). The array has
2 min read
All reverse permutations of an array using STL in C++ Given an array, the task is to print or display all the reverse permutations of this array using STL in C++. Reverse permutation means, for an array {1, 2, 3}: forward permutations: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 reverse permutations: 3 2 1 3 1 2 2 3 1 2 1 3 1 3 2 1 2 3 Examples: Input: a[] = {
3 min read
Working with Array and Vectors using STL in C++ Using STL library it is very easy to perform certain basic operations on array like Sorting, searching, sum of elements, finding minimum and maximum element of the array. Sorting Sorting can be done with the help of sort() function. sort(starting_index, last_index) â To sort the given array/vector.
6 min read
How to Sort an Array in C++? Sorting an array involves rearranging its elements in a specific order such as from smallest to largest element or from largest to smallest element, etc. In this article, we will learn how to sort an array in C++.Example:Input: arr ={5,4,1,2,3}Output: 1 2 3 4 5Explanation: arr is sorted in increasin
4 min read