std::sample(3)
π NAME π
okay so bestie, this is std::sample β and yes, the name is literally std::sample. the manual page is std::sample(3) from the C++ Standard Library, and it is giving random-sampling main character energy. no cap. β¨
β¨ Synopsis β¨
Defined in header <algorithm>
template< class PopulationIt, class SampleIt, class Distance, class URBG >
SampleIterator sample( PopulationIt first, PopulationIt last,
SampleIt out, Distance n, URBG&& g );
(since C++17), bestie. π«Ά
So what does it do? It selects n elements from the sequence [first, last) (without replacement) such that each possible sample has equal probability of appearance, and writes those selected elements into the output iterator out. Random numbers are generated using the random number generator g.
If n is greater than the number of elements in the sequence, it selects all elements in the sequence. slay.
The algorithm is stable (preserves the relative order of the selected elements) only if PopulationIt meets the requirements of LegacyForwardIterator. so like, stability is not automatic, bestie.
Also: if the value type of first (until C++20) or *first (since C++20) is not writable to out, the program is ill-formed. not a vibe check you want to fail π
If any of the following conditions is satisfied, the behavior is undefined β and UB is not a cute look:
outis in[first, last).PopulationItdoes not meet the requirements ofLegacyInputIterator.SampleItdoes not meet the requirements ofLegacyOutputIterator.- All following conditions are satisfied:
PopulationItdoes not meet the requirements ofLegacyForwardIterator.(until C++23)PopulationItdoes not modelforward_iterator.(since C++23)SampleItdoes not meet the requirements ofLegacyRandomAccessIterator.
- Given the type
Tasstd::remove_reference_t<URBG>, any of the following conditions is satisfied:Tdoes not meet the requirements ofUniformRandomBitGenerator.- The return type of
Tis not convertible toDistance.(until C++20)
πΈ Parameters πΈ
first,lastβ pair of iterators forming the range from which to make the sampling (the population).outβ the output iterator where the samples are written.nβ number of samples to make.gβ the random number generator used as the source of randomness.
π Type requirements π
Distancemust be an integer type.
π¦ Return value π¦
Returns a copy of out after the last sample that was output, that is, end of the sample range. so it hands you back the end of the sample range, periodt. π
β Complexity β
Linear in std::distance(first, last). lowkey tidy. β¨
π Possible implementation π
See the implementations in libstdc++, libc++ and MSVC STL. the page is just pointing you at the real ones, iykyk.
π Notes π
This function may implement selection sampling or reservoir sampling. literally either vibe.
Feature-test macro table, translated into bestie bullets because tables are not the moment:
- Feature-test macro:
__cpp_lib_sample - Value:
201603L - Std:
(C++17) - Feature:
std::sample
π Example π
the page marks this // Run this code, so here it is exactly:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <random>
#include <string>
int main()
{
std::string in {"ABCDEFGHIJK"}, out;
std::sample(in.begin(), in.end(), std::back_inserter(out), 4,
std::mt19937 {std::random_device{}()});
std::cout << "Four random letters out of " << in << " : " << out << '\n';
}
Possible output: Four random letters out of ABCDEFGHIJK: EFGK
random, so your letters may absolutely serve a different look. π
π See also π
random_shuffleβ randomly re-orders elements in a range(until C++17)(function template)shuffleβ randomly re-orders elements in a range(C++11)(function template)ranges::sampleβ selects N random elements from a sequence(C++20)(niebloid)
mwah π that's the whole page, bestie. footer sign-off: source/version http://cppreference.com, date 2024.06.10, page std::sample(3). periodt. π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms