Home C++ Algorithms Functions random_shuffle() C++ std::random_shuffle() Function 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.
std::random_shuffle(v.begin(), v.end());
std::random_shuffle(v.begin(), v.end(), rng); Call What 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 // 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 Parameter Description first, last Required. The range to shuffle. r Optional. A function that takes n and returns a random number from 0 to n - 1.
Return Value Return Value Description voidNothing is returned.
C++ Examples 1. Shuffle then sort 2. The modern replacement 3. Pass your own function Example 1 — Shuffle then sort Shuffle a vector and sort it back to prove nothing was lost.
#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 std::srand(1) sets the starting point for rand().random_shuffle() mixes the five values.Sorting puts them back in order, so we can see nothing was lost. Example 2 — The modern replacement The same job with std::shuffle.
#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 std::mt19937 is a good random engine from <random>.std::shuffle() needs that engine as its third argument.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.
#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 pickIndex(n) must return a number from 0 up to n - 1.random_shuffle() calls it while it swaps elements.The order is random, but the sum is always 15. Frequently Asked Questions What does std::random_shuffle() do? In C++, std::random_shuffle() randomly reorders a range using rand() (removed in C++17). Include <algorithm> to use it.
Which header and C++ version do I need for std::random_shuffle()? 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.
What does std::random_shuffle() return? It returns nothing (void). The range is shuffled in place. The function was removed in C++17.
Why was random_shuffle() removed? It relied on std::rand(), which is a poor random source and not thread safe. std::shuffle() with a modern engine replaced it.
What replaces random_shuffle()? 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 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.
7 people found this page helpful
Helpful Share Copy link Suggestion