C++ Standard Library C++ STL Library

C++ <algorithm> - random_shuffle() Function



The C++ algorithm::random_shuffle function is used to randomly shuffle the elements in the range [first,last). The function rearranges elements based on default random number generator (in first version) or gen (in second version).

Syntax

//default generator version
template <class RandomAccessIterator>
  void random_shuffle (RandomAccessIterator first, 
                       RandomAccessIterator last);

//specific generator version
template <class RandomAccessIterator, class RandomNumberGenerator>
  void random_shuffle (RandomAccessIterator first, 
                       RandomAccessIterator last,
                       RandomNumberGenerator& gen);
//default generator version
template <class RandomAccessIterator>
  void random_shuffle (RandomAccessIterator first, 
                       RandomAccessIterator last);

//specific generator version
template <class RandomAccessIterator, class RandomNumberGenerator>
  void random_shuffle (RandomAccessIterator first, 
                       RandomAccessIterator last,
                       RandomNumberGenerator&& gen);

Parameters

first Specify initial position of the random-access iterators of the sequence to be shuffled. The range used is [first,last).
last Specify final position of the random-access iterators of the sequence to be shuffled. The range used is [first,last).
gen An unary function that takes one element in the range as argument and returns random number.

Return Value

None.

Time Complexity

Linear i.e, Θ(n).

Example:

In the example below, the algorithm::random_shuffle function is used to randomly shuffle the elements in the given range.

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main (){
  vector<int> vec= {10, 20, 30, 40, 50, 60, 70, 80};
  vector<int>::iterator it;

  cout<<"vec contains: ";
  for(it = vec.begin(); it != vec.end(); ++it)
    cout<<" "<<*it;

  //randomly shuffle the elements of the 
  //vector using default generator
  random_shuffle(vec.begin(), vec.end());

  cout<<"\nvec contains: ";
  for(it = vec.begin(); it != vec.end(); ++it)
    cout<<" "<<*it;

  return 0;
}

One of the possible output of the above code could be:

vec contains:  10 20 30 40 50 60 70 80
vec contains:  50 40 80 70 10 60 30 20

Example:

The example below shows how to use gen with algorithm::random_shuffle function.

#include <iostream>
#include <algorithm>
#include <vector>
#include <stdlib.h>     //srand, rand
#include <ctime>        //time
using namespace std;

float mygen (int i) { 
  return rand()%i;
}

int main (){
  //initialize random seed
  srand (time(NULL));

  vector<int> vec= {10, 20, 30, 40, 50, 60, 70, 80};
  vector<int>::iterator it;

  cout<<"vec contains: ";
  for(it = vec.begin(); it != vec.end(); ++it)
    cout<<" "<<*it;

  //randomly shuffle the elements of the vector
  //using mygen as random number generator
  random_shuffle(vec.begin(), vec.end(), mygen);

  cout<<"\nvec contains: ";
  for(it = vec.begin(); it != vec.end(); ++it)
    cout<<" "<<*it;

  return 0;
}

The output of the above code will be:

vec contains:  10 20 30 40 50 60 70 80
vec contains:  20 40 60 30 50 70 80 10

❮ C++ <algorithm> Library