Shuffle
Shuffle() function rearranges the elements in a range by putting them randomly, using g as a uniform random number generator.
Parameters:
first: An iterator that points to the position of the first element in the range to be rearranged.
last: An iterator that points to the position of the last element in the range to be rearranged.
g: A special function object called a uniform random number generator.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
//initializing the vector
vector<char> s{ 'c', 'o', 'd', 'i', 'n', 'g', ' ', 'n', 'i', 'n', 'j', 'a', 's' };
cout<<"shuffling the elements by using shuffle()"<<endl;
//printing the original vector
cout<<"original vector : ";
for (int i = 0; i < s.size(); i++)
cout << s[i];
cout << endl;
// To obtain a time-based seed
unsigned seed = 0;
// Using shuffle
shuffle(s.begin(), s.end(), default_random_engine(seed));
// printing the shuffled vector
cout << "after shuffling: ";
for (int i = 0; i < s.size(); i++)
cout << s[i];
cout << endl;
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output:

You can also compile this code with the help of Online C++ Compiler
Click on the following link to read further: Features of C Language
Random Shuffle
It randomly arranges the elements in a given range. It swaps the value of each element with some other element picked randomly. The random element is determined by the function gen.
Parameters
first: An iterator that points to the position of the first element in the range to be rearranged.
last: An iterator that points to the position of the last element in the range to be rearranged.
result: An output iterator that points to the first element's position in the destination range.
gen: A special function object called a random number generator.
Time complexity: O(n)
Code:
#include <bits/stdc++.h>
using namespace std;
// random generator function
int randomfunc(int n)
{
return rand() % n;
}
int main()
{
srand(unsigned(time(0)));
cout<<"shuffling the elements by using random_shuffle()"<<endl;
//initializing the vector
vector<char> arr{ 'c', 'o', 'd', 'i', 'n', 'g', ' ', 'n', 'i', 'n', 'j', 'a', 's' };
cout<<"original vector : ";
for (int i = 0; i < arr.size(); i++)
cout << arr[i]<< endl;
// using built-in random generator
random_shuffle(arr.begin(), arr.end());
// using random_shuffle
random_shuffle(arr.begin(), arr.end(), randomfunc);
// printing out
cout << "after shuffling: ";
for (int i = 0; i < arr.size(); i++)
cout << arr[i]<< endl;
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output:

Check out this problem - First And Last Occurrences Of X
FAQ
-
What is preferable, random_shuffle or shuffle?
Shuffle uses a better random generator, and hence, shuffle is preferable.
-
Are there any other ways to shuffle the elements?
Yes, there are many algorithms by which we can shuffle the elements. For example, Fisher-Yates Shuffle Algorithm.
-
What other functions are provided by the <algorithm> library?
<algorithm> provides various functions on searching, sorting, counting, etc.
Key Takeaways
We talked about shuffle() and random_shuffle() in this article. We looked at the main difference between them and also implemented them. To know more about different functions in C++, check out this blog. Check out C++ interview questions to learn about the common interview questions asked for C++. You can get complete knowledge about C++ data structures and more on coding ninjas.
Recommended Readings: