C++ std::sample() Function

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

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.

C++
std::sample(pop.begin(), pop.end(), std::back_inserter(out), 3, gen);
std::mt19937 gen(std::random_device{}());
CallWhat 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
C++
template<class PopulationIt, class SampleIt, class Distance, class URBG>
SampleIt sample(PopulationIt first, PopulationIt last,
                SampleIt out, Distance n, URBG&& g);

Parameters

ParameterDescription
first, lastRequired. The range to pick from.
outRequired. Where the chosen elements are written.
nRequired. How many elements to pick. If the range is smaller, all elements are returned.
gRequired. A random generator such as std::mt19937.

Return Value

Return ValueDescription
IteratorPoints just after the last element written to the output.

C++ Examples

Example 1 — Pick three numbers

Choose three random numbers from ten.

C++
#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

  1. The population holds ten numbers.
  2. sample() picks three of them at random and appends them to out.
  3. 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?

C++
#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

  1. The request is for ten elements, but there are only four.
  2. sample() cannot pick an element twice.
  3. So it returns all four elements.

Example 3 — Order is kept

A vector keeps the original order in the sample.

C++
#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

  1. With forward iterators, sample() keeps the relative order of the chosen elements.
  2. The population is sorted, so the sample should be sorted too.
  3. std::is_sorted() confirms this with true.

Frequently Asked Questions

In C++, std::sample() picks n random elements from a range. Include <algorithm> to use it.
Include <algorithm>. It needs C++17 or newer, so compile with -std=c++17 or later.
It returns an iterator to the position after the last sampled element in the output.
No. It picks each element at most once, so it is sampling without replacement.
All elements of the range are copied to the output.

Next: search()

Finds the first occurrence of a sequence inside a range.

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

21 people found this page helpful