## random_sample
## Prototype
template <class InputIterator, class RandomAccessIterator> Random AccessIterator random_sample(InputIterator first, InputIterator last, RandomAccessIterator ofirst, RandomAccessIterator olast) template <class InputIterator, class RandomAccessIterator, class RandomNumberGenerator> random_sample(InputIterator first, InputIterator last, RandomAccessIterator ofirst, RandomAccessIterator olast, 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: -
`InputIterator` is a model of InputIterator -
`RandomAccessIterator` is a model of RandomAccessIterator -
`RandomAccessIterator` is mutable. -
`InputIterator` 's value type is convertible to`RandomAccessIterator` 's value type.
For the second version: -
`InputIterator` is a model of InputIterator -
`RandomAccessIterator` is a model of RandomAccessIterator -
`RandomAccessIterator` is mutable. -
`RandomNumberGenerator` is a model of RandomNumberGenerator -
`InputIterator` 's value type is convertible to`RandomAccessIterator` 's value type. -
`RandomAccessIterator` 's distance type is convertible to`RandomNumberGenerator` 's argument type.
## Preconditions-
`[first, last)` is a valid range. -
`[ofirst, olast)` is a valid range. -
`[first, last)` and`[ofirst, olast)` do not overlap. -
`last - first` is less than`rand` 's maximum value.
## ComplexityLinear in ## Exampleint main() { const int N = 10; const int n = 4; int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int B[n]; random_sample(A, A+N, B, B+n); copy(B, B + n, ostream_iterator<int>(cout, " ")); // The printed value might be 1 6 3 5, // or any of 5039 other possibilities. } ## Notes[1] This is "Algorithm R" from section 3.4.2 of Knuth (D. E. Knuth, [2] If preservation of the relative ordering within the input range is important for your application, you should use ## See also |