Files
cppdraft_translate/cppdraft/alg/random/sample.md
2025-10-25 03:02:53 +03:00

6.3 KiB

[alg.random.sample]

26 Algorithms library [algorithms]

26.7 Mutating sequence operations [alg.modifying.operations]

26.7.12 Sample [alg.random.sample]

🔗

`template<class PopulationIterator, class SampleIterator, class Distance, class UniformRandomBitGenerator> SampleIterator sample(PopulationIterator first, PopulationIterator last, SampleIterator out, Distance n, UniformRandomBitGenerator&& g);

template<input_iterator I, sentinel_for S, weakly_incrementable O, class Gen> requires (forward_iterator || random_access_iterator) && indirectly_copyable<I, O> && uniform_random_bit_generator<remove_reference_t> O ranges::sample(I first, S last, O out, iter_difference_t n, Gen&& g); template<input_range R, weakly_incrementable O, class Gen> requires (forward_range || random_access_iterator) && indirectly_copyable<iterator_t, O> && uniform_random_bit_generator<remove_reference_t> O ranges::sample(R&& r, O out, range_difference_t n, Gen&& g); `

1

#

Mandates: For the overload in namespace std,Distance is an integer type and*first is writable ([iterator.requirements.general]) to out.

2

#

Preconditions: out is not in the range [first, last).

For the overload in namespace std:

3

#

Effects: Copies min(last - first, n) elements (the sample) from [first, last) (the population) to out such that each possible sample has equal probability of appearance.

[Note 1:

Algorithms that obtain such effects include selection sampling and reservoir sampling.

— end note]

4

#

Returns: The end of the resulting sample range.

5

#

Complexity: O(last - first).

6

#

Remarks:

  • (6.1)

    For the overload in namespace std, stable if and only if PopulationIterator models forward_iterator. For the first overload in namespace ranges, stable if and only if I models forward_iterator.

  • (6.2)

    To the extent that the implementation of this function makes use of random numbers, the object g serves as the implementation's source of randomness.