C++ Reference ยท Algorithms
The std::shuffle and std::sample algorithms
Reference for C++ std::shuffle and std::sample: randomize a range with a modern engine, pick random elements, seeding, and why random_shuffle is gone.
std::shuffle rearranges a range into a random order using a uniform random bit generator that you supply, such as std::mt19937. std::sample (C++17) picks n random elements from a range without repeating any, and writes them to an output iterator.
The old std::random_shuffle used rand() and was removed in C++17. Always pass an engine to shuffle. The engine decides the sequence: with a fixed seed you get the same permutation on the same standard library every time, but the exact result differs between library implementations, so never rely on specific values across platforms.
std::mt19937 rng(42); std::shuffle(v.begin(), v.end(), rng);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
template<class RandomIt, class URBG> void shuffle(RandomIt first, RandomIt last, URBG&& g); template<class PopIt, class SampleIt, class Distance, class URBG> SampleIt sample(PopIt first, PopIt last, SampleIt out, Distance n, URBG&& g); // C++17
| Name | Description |
|---|---|
first, last |
The range to shuffle, or the population to sample from. |
g |
A uniform random bit generator such as std::mt19937 or std::default_random_engine. Passed by forwarding reference and advanced by the call. |
out |
Output iterator that receives the sampled elements. |
n |
The number of elements to pick. If it exceeds the population size, all elements are taken. |
Returns: shuffle returns nothing. sample returns the output iterator one past the last element written.
Errors / undefined behavior: shuffle needs random-access iterators and a valid engine. For sample, the output range must not overlap the input. Using the same engine from several threads without synchronization is a data race.
Usage notes
- Seeding:
std::mt19937 rng(std::random_device{}())gives a different sequence every run; a fixed integer gives a repeatable one.std::random_devicemay be deterministic on some platforms. - Distribution results are not portable. The sequences of engines like
mt19937are standardized, butshuffleand distributions may consume the numbers differently in libstdc++, libc++ and MSVC. Do not store expected shuffled output in tests; test properties instead (same elements, same size). - Keep one engine alive. Creating a new
mt19937seeded with the same value before every shuffle yields the same permutation each time. - Complexity:
shufflemakes exactlylast - first - 1swaps (Fisher-Yates), so it is O(n).sampleis O(n) in the population size. sampleorder: it is stable if the population iterator is a forward iterator (or the output is random access), meaning the picked elements keep their source order.sampleis not itself a shuffle; callshuffleon the result if you need a random order.rand()andrandom_shuffle: do not use them;random_shufflewas deprecated in C++14 and removed in C++17.- Ranges:
std::ranges::shuffle(C++20) takes a range and an engine.