Table of contents
1.
Introduction
2.
Shuffle vs Random_shuffle
3.
Shuffle
4.
Random Shuffle
5.
FAQ
6.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Shuffle vs random_shuffle in C++

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

C++ offers various functions that we can use to process our data according to our needs. Shuffle, and random_shuffle are the functions used to shuffle the elements by implementing a random permutation feature. These are part of C++ <algorithm> library. Let us look at these functions and the difference between them.

source

Shuffle vs Random_shuffle

The main difference between random_shuffle and shuffle is that random_shuffle uses the rand() function while shuffle uses uniform random number generator (urng) to shuffle the elements.

rand() produces a pseudo-random number in the closed interval [0, RAND_MAX] and urng generates uniformly distributed random numbers over an interval that you specify.

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

  1. What is preferable, random_shuffle or shuffle?
    Shuffle uses a better random generator, and hence, shuffle is preferable.
  2.  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.
  3. 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:

Live masterclass