## random_sample_n
## Prototype
template <class ForwardIterator, class OutputIterator, class Distance> OutputIterator random_sample_n(ForwardIterator first, ForwardIterator last, OutputIterator out, Distance n) template <class ForwardIterator, class OutputIterator, class Distance, class RandomNumberGenerator> OutputIterator random_sample_n(ForwardIterator first, ForwardIterator last, OutputIterator out, Distance n, RandomNumberGenerator& rand) ## Description
The first version uses an internal random number generator, and the second uses a RandomNumberGenerator, a special kind of functors, that is explicitly passed as an argument. ## DefinitionDefined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h. This function is an SGI extension; it is not part of the C++ standard. ## Requirements on typesFor the first version: -
`ForwardIterator` is a model of ForwardIterator -
`OutputIterator` is a model of OutputIterator -
`ForwardIterator` 's value type is convertible to a type in`OutputIterator` 's set of value types. -
`Distance` is an integral type that is large enough to represent the value`last - first` .
For the second version: -
`ForwardIterator` is a model of ForwardIterator -
`OutputIterator` is a model of OutputIterator -
`RandomNumberGenerator` is a model of RandomNumberGenerator -
`Distance` is an integral type that is large enough to represent the value`last - first` . -
`ForwardIterator` 's value type is convertible to a type in`OutputIterator` 's set of value types. -
`Distance` is convertible to`RandomNumberGenerator` 's argument type.
## Preconditions-
`[first, last)` is a valid range. -
`n` is nonnegative. -
`[first, last)` and`[out, out + n)` do not overlap. -
There is enough space to hold all of the elements being copied. More formally, the requirement is that
`[out, out + min(n, last - first))` is a valid range. -
`last - first` is less than`rand` 's maximum value.
## ComplexityLinear in ## Exampleint main() { const int N = 10; int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; random_sample_n(A, A+N, ostream_iterator<int>(cout, " "), 4); // The printed value might be 3 5 6 10, // or any of 209 other possibilities. } ## Notes[1] This is "Algorithm S" from section 3.4.2 of Knuth (D. E. Knuth, [2] In contrast, the ## See also |