C++ std::shuffle() Function

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

Definition

In C++, std::shuffle() puts the elements of a range in a random order. It needs a uniform random bit generator (URBG), such as std::mt19937.

This is the modern way to shuffle. It replaces the older std::random_shuffle(), which was removed in C++17.

Syntax

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

C++
std::mt19937 gen(std::random_device{}());
std::shuffle(v.begin(), v.end(), gen);
CallWhat it does
std::mt19937 gen(std::random_device{}());Create a random engine with a random seed.
std::shuffle(v.begin(), v.end(), gen);Shuffle the vector with that engine.
Library declarations
C++
template<class RandomIt, class URBG>
void shuffle(RandomIt first, RandomIt last, URBG&& g);

Parameters

ParameterDescription
first, lastRequired. The range to shuffle. It needs random access iterators.
gRequired. A random generator, for example std::mt19937.

Return Value

Return ValueDescription
voidNothing is returned. The range is shuffled in place.

C++ Examples

Example 1 — Same elements, new order

Shuffle a vector and prove that no value was lost.

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

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

    std::mt19937 gen(42);
    std::shuffle(v.begin(), v.end(), gen);

    std::sort(v.begin(), v.end());
    std::cout << std::boolalpha << "Same elements: " << (v == original) << '\n';
    return 0;
}

How It Works

  1. std::mt19937 gen(42) creates a random engine with a fixed seed.
  2. shuffle() changes the order, and we sort the result to compare it with the copy.
  3. The vectors match, which shows shuffling only moves values around.

Example 2 — Shuffle a deck

Number cards 1 to 10, shuffle, and check the sum.

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

int main() {
    std::vector<int> deck(10);
    std::iota(deck.begin(), deck.end(), 1);

    std::random_device rd;
    std::mt19937 gen(rd());
    std::shuffle(deck.begin(), deck.end(), gen);

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

How It Works

  1. std::iota() fills the deck with 1, 2, 3 up to 10.
  2. std::random_device gives a different seed on each run, so the order changes every time.
  3. The sum is always 55, because the same ten cards are still there.

Example 3 — Keep the first card

Shuffle only a part of the range.

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

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

    std::mt19937 gen(7);
    std::shuffle(v.begin() + 1, v.end(), gen);

    std::cout << "First element stays: " << v[0] << '\n';
    std::cout << "Size: " << v.size() << '\n';
    return 0;
}

How It Works

  1. The range starts at v.begin() + 1, so index 0 is outside it.
  2. Only the last five elements are mixed.
  3. The first element is guaranteed to stay 1.

Frequently Asked Questions

In C++, std::shuffle() randomly reorders the elements of a range using a random engine. Include <algorithm> to use it.
Include <algorithm>. It needs C++11 or newer, so compile with -std=c++11 or later.
It returns nothing (void). The elements are put in a random order.
The engine gives high quality random numbers. It also lets you choose a fixed seed when you want the same result every time.
Use std::shuffle() with std::mt19937. random_shuffle() was removed in C++17.

Next: sort()

Sorts the elements of a range into ascending order.

sort() 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.

17 people found this page helpful