Home C++ Algorithms Functions sample() C++ std::sample() Function Definition In C++17, std::sample() selects up to n random elements from a source range and writes them to an output. Each element can be chosen at most once.
It needs a random engine such as std::mt19937. It is perfect for tasks like picking three winners or testing a random part of your data.
Syntax Add #include <algorithm>, then call std::sample(). Start with these two lines.
std::sample(pop.begin(), pop.end(), std::back_inserter(out), 3, gen);
std::mt19937 gen(std::random_device{}()); Call What it does std::sample(pop.begin(), pop.end(), std::back_inserter(out), 3, gen);Pick three random elements. std::mt19937 gen(std::random_device{}());Create the random engine.
Compile with -std=c++17 or newer.
Library declarations template<class PopulationIt, class SampleIt, class Distance, class URBG>
SampleIt sample(PopulationIt first, PopulationIt last,
SampleIt out, Distance n, URBG&& g); Parameters Parameter Description first, last Required. The range to pick from. out Required. Where the chosen elements are written. n Required. How many elements to pick. If the range is smaller, all elements are returned. g Required. A random generator such as std::mt19937.
Return Value Return Value Description Iterator Points just after the last element written to the output.
C++ Examples 1. Pick three numbers 2. Ask for too many 3. Order is kept Example 1 — Pick three numbers Choose three random numbers from ten.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>
int main() {
std::vector<int> pop = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<int> out;
std::mt19937 gen(std::random_device{}());
std::sample(pop.begin(), pop.end(), std::back_inserter(out), 3, gen);
std::cout << "Sample size: " << out.size() << '\n';
return 0;
} How It Works The population holds ten numbers. sample() picks three of them at random and appends them to out.Which three change each run, but the size is always 3. Example 2 — Ask for too many What if n is bigger than the range?
#include <algorithm>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>
int main() {
std::vector<int> pop = {10, 20, 30, 40};
std::vector<int> out;
std::mt19937 gen(1);
std::sample(pop.begin(), pop.end(), std::back_inserter(out), 10, gen);
std::cout << "Sample size: " << out.size() << '\n';
return 0;
} How It Works The request is for ten elements, but there are only four. sample() cannot pick an element twice.So it returns all four elements. Example 3 — Order is kept A vector keeps the original order in the sample.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>
int main() {
std::vector<int> pop = {1, 2, 3, 4, 5, 6, 7, 8};
std::vector<int> out;
std::mt19937 gen(std::random_device{}());
std::sample(pop.begin(), pop.end(), std::back_inserter(out), 4, gen);
std::cout << std::boolalpha << "Still in order: "
<< std::is_sorted(out.begin(), out.end()) << '\n';
return 0;
} How It Works With forward iterators, sample() keeps the relative order of the chosen elements. The population is sorted, so the sample should be sorted too. std::is_sorted() confirms this with true.Frequently Asked Questions What does std::sample() do? In C++, std::sample() picks n random elements from a range. Include <algorithm> to use it.
Which header and C++ version do I need for std::sample()? Include <algorithm>. It needs C++17 or newer, so compile with -std=c++17 or later.
What does std::sample() return? It returns an iterator to the position after the last sampled element in the output.
Can sample() pick the same element twice? No. It picks each element at most once, so it is sampling without replacement.
What if n is larger than the range? All elements of the range are copied to the output.
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.
21 people found this page helpful
Helpful Share Copy link Suggestion