cpp/algorithm/sample

Selects elements from the sequence  (without replacement) such that each possible sample has equal probability of appearance, and writes those selected elements into the output iterator. Random numbers are generated using the random number generator.

If is greater than the number of elements in the sequence, selects  elements.

The algorithm is stable (preserves the relative order of the selected elements) only if meets the requirements of

The behavior is undefined if is in.

Return value
Returns a copy of after the last sample that was output, that is, end of the sample range.

Complexity
Linear in.

Possible implementation
See the implementations in libstdc++, libc++ and MSVC STL.