C++ std::random_shuffle() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

Definition

In C++, std::random_shuffle() is the older way to mix up a range. It uses std::rand() or a function you give it. It was deprecated in C++14 and removed in C++17.

New code should use std::shuffle() with a random engine such as std::mt19937. The examples here compile with -std=c++14 or older and may show a deprecation warning.

Syntax

Add #include <algorithm>, then call std::random_shuffle(). Start with these two lines.

C++
std::random_shuffle(v.begin(), v.end());
std::random_shuffle(v.begin(), v.end(), rng);
CallWhat it does
std::random_shuffle(v.begin(), v.end());Shuffle with the built-in random source.
std::random_shuffle(v.begin(), v.end(), rng);Use your own function that returns a number below n.

Removed from the standard in C++17. Prefer std::shuffle().

Library declarations
C++
// Removed in C++17
template<class RandomIt>
void random_shuffle(RandomIt first, RandomIt last);

template<class RandomIt, class RandomFunc>
void random_shuffle(RandomIt first, RandomIt last, RandomFunc&& r);

Parameters

ParameterDescription
first, lastRequired. The range to shuffle.
rOptional. A function that takes n and returns a random number from 0 to n - 1.

Return Value

Return ValueDescription
voidNothing is returned.

C++ Examples

Example 1 — Shuffle then sort

Shuffle a vector and sort it back to prove nothing was lost.

C++
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>

int main() {
    std::srand(1);
    std::vector<int> v = {1, 2, 3, 4, 5};

    std::random_shuffle(v.begin(), v.end());

    std::sort(v.begin(), v.end());
    std::cout << "After sorting: ";
    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. std::srand(1) sets the starting point for rand().
  2. random_shuffle() mixes the five values.
  3. Sorting puts them back in order, so we can see nothing was lost.

Example 2 — The modern replacement

The same job with std::shuffle.

C++
#include <algorithm>
#include <iostream>
#include <numeric>
#include <random>
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5};

    std::mt19937 gen(std::random_device{}());
    std::shuffle(v.begin(), v.end(), gen);

    int sum = std::accumulate(v.begin(), v.end(), 0);
    std::cout << "Shuffled " << v.size() << " items, sum " << sum << '\n';
    return 0;
}

How It Works

  1. std::mt19937 is a good random engine from <random>.
  2. std::shuffle() needs that engine as its third argument.
  3. The sum stays 15, because shuffling does not change the values.

Example 3 — Pass your own function

Give random_shuffle a function that picks the index.

C++
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <vector>

int pickIndex(int n) {
    return std::rand() % n;
}

int main() {
    std::srand(3);
    std::vector<int> v = {1, 2, 3, 4, 5};

    std::random_shuffle(v.begin(), v.end(), pickIndex);

    int sum = std::accumulate(v.begin(), v.end(), 0);
    std::cout << "Sum: " << sum << '\n';
    return 0;
}

How It Works

  1. pickIndex(n) must return a number from 0 up to n - 1.
  2. random_shuffle() calls it while it swaps elements.
  3. The order is random, but the sum is always 15.

Frequently Asked Questions

In C++, std::random_shuffle() randomly reorders a range using rand() (removed in C++17). Include <algorithm> to use it.
Include <algorithm>. random_shuffle() exists since C++98, was deprecated in C++14, and was removed in C++17. Use std::shuffle() in new code.
It returns nothing (void). The range is shuffled in place. The function was removed in C++17.
It relied on std::rand(), which is a poor random source and not thread safe. std::shuffle() with a modern engine replaced it.
std::shuffle(first, last, engine) with an engine such as std::mt19937.

Next: remove()

Moves all elements not equal to a value to the front and returns the new end.

remove() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful