C++ std::shuffle() Function

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.
std::mt19937 gen(std::random_device{}());
std::shuffle(v.begin(), v.end(), gen);
| Call | What 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
template<class RandomIt, class URBG>
void shuffle(RandomIt first, RandomIt last, URBG&& g);
Parameters
| Parameter | Description |
|---|
| first, last | Required. The range to shuffle. It needs random access iterators. |
| g | Required. A random generator, for example std::mt19937. |
Return Value
| Return Value | Description |
|---|
void | Nothing 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.
#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
std::mt19937 gen(42) creates a random engine with a fixed seed.shuffle() changes the order, and we sort the result to compare it with the copy.- 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.
#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
std::iota() fills the deck with 1, 2, 3 up to 10.std::random_device gives a different seed on each run, so the order changes every time.- 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.
#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;
}
First element stays: 1
Size: 6
How It Works
- The range starts at
v.begin() + 1, so index 0 is outside it. - Only the last five elements are mixed.
- 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.
About the author
Developer, cloud engineer, and technical writer
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