C++

Shuffle() vs random_shuffle() in C++

In C++, the standard library provides two functions, shuffle() and random_shuffle() that are used to rearrange the elements of a container. Although both functions serve the same purpose, they differ in their implementation and the way they generate random numbers.

From this article, you will find the differences between these two functions and understand how they work.

shuffle() in C++

The shuffle() function is a built-in C++ function used to randomly shuffle or rearrange the elements in a given range. The function is declared in the <algorithm> header file and has two arguments: the starting position of the range is the first argument, and the second argument represents the ending position.

In addition, it also takes an optional third parameter, which is a function object that generates random numbers to be used for shuffling the elements in the range.

When the shuffle() function is called, it randomly reorders the elements in the specified range using the provided random number generator. The result of the shuffle is not predictable, and each possible permutation of the elements is equally likely to occur.

Example

Consider the below example of using the shuffle() function in C++. In this program, we have created the vector vec with the integer values of 0 to 10. Then we generate a random number generator, which is then passed along with the vector’s range to the shuffle() function. The shuffle() function takes the number and swaps the elements based on this number. Then we printed the rearranged vector sequence using the for loop

#include <iostream>

#include <vector>

#include <algorithm>

#include <random>

#include <chrono>

using namespace std;

int main()

{

  vector<int> vec {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

  unsigned seed = chrono::system_clock::now().time_since_epoch().count();

  shuffle(vec.begin(), vec.end(), default_random_engine(seed));

  cout << "shuffled elements are:";

  for (int& i : vec)

  cout << ' ' << i;

  cout << endl;

  return 0;

}

random_shuffle() in C++

The random_shuffle() function also randomly rearranges the elements in the given range with some randomly picked number. It uses a random number generator to generate a sequence of random numbers and then uses those numbers to shuffle the elements in the range, so the sequence of the program will be different every time you run the program.

Two parameters are required for random_shuffle(): the starting position of the range is the first parameter, and the second parameter is the ending position. Additionally, random_shuffle() can take an optional third parameter, which is a function object that can be used to generate the random numbers for shuffling the elements.

Example

The below example illustrates the working of the random_shuffle() in C++. In this code, we have created a vector vec with integers values from 1 to 10 and then used the for loop to print the randomly shuffled sequence:

#include <iostream>

#include <algorithm>

using namespace std;

int main()

{

  vector<int> vec {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

  srand(static_cast<unsigned int>(time(nullptr)));

  random_shuffle(vec.begin(), vec.end());

  for (int i : vec) {

  cout << i << " ";

  }

  cout << '\n';

   

  return 0;

}

Difference Between shuffle() and random_shuffle()

Here are the key differences between shuffle() and random_shuffle() functions in C++.

1: random_shuffle() takes a pair of iterators representing the range of elements to shuffle, while shuffle() takes a pair of iterators representing the range of elements to shuffle, as well as a random number generator to use for shuffling.

2: random_shuffle() is generally less efficient than shuffle(), as it has to generate a sequence of random numbers to use for shuffling.

3: random_shuffle() uses the C++ Standard Library’s internal implementation of the random number generator to shuffle the elements, while shuffle() allows you to specify your own random number generator to use for shuffling, giving you more control over the randomness of the shuffling.

4: random_shuffle() was introduced in C++98 and is supported by all versions of the C++ Standard Library, while shuffle() was introduced in C++11 and is only supported by compilers that implement that version of the standard.

Final Thoughts

The choice between shuffle() and random_shuffle() depends on your specific use case and requirements. If you need more control over the randomness of the shuffling, or if you want to use a custom random number generator, then shuffle() would be a better choice. On the other hand, if you don’t need that level of control and just want a simple way to shuffle elements, then random_shuffle() could be sufficient.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.