std::random_shuffle

From Cppreference

Jump to: navigation, search
Defined in header <algorithm>

template< class RandomAccessIterator >
void random_shuffle( RandomAccessIterator first, RandomAccessIterator last );
(1)
template< class RandomAccessIterator, RandomNumberGenerator r >
void random_shuffle( RandomAccessIterator first, RandomAccessIterator last, RandomNumberGenerator& r );
(2)

Reorders the elements in the given range [first, last) in random order. One version of the function uses internal implementation-defined random number generator, another uses the given random number generator r.

Contents

Parameters

first, last - the range of elements to shuffle randomly
r - function, returning a random number not greater than its argument on each invocation

Return value

(none)

Complexity

linear in the distance between first and last

Equivalent function

template< class RandomAccessIterator, RandomNumberGenerator r >
void random_shuffle( RandomAccessIterator first, RandomAccessIterator last, RandomNumberGenerator& r )
{
    std::iterator_traits<RandomAccessIterator>::difference_type i, n;
    n = last - first;
    for (i = n-1; i > 0; --i) {
        std::swap(first[i], first[r(i)]);
    }
}

Example

See also

next_permutation
generates the next greater lexicographic permutation of a range of elements
(function template)
prev_permutation
generates the next smaller lexicographic permutation of a range of elements
(function template)
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox
In other languages