next up previous index
Next: Partition with elements Up: Mutating sequence operations Previous: Rotate elements in

Randomly shuffle elements in a sequence

Source code

Declaration

template <class RandomAccessIterator>
inline void random_shuffle(RandomAccessIterator first,
                           RandomAccessIterator last);

template <class RandomAccessIterator, class RandomNumberGenerator>
void random_shuffle(RandomAccessIterator first, RandomAccessIterator last,
                    RandomNumberGenerator& rand);

Description   The random_shuffle  functions randomly shuffle   the elements in the range [ first, last) with uniform distribution  .

Type requirements

Group Mutating sequence operations.

Time complexity Linear.

The number of swaps  performed is one less than the size of the range [ first, last) .

Space complexity Constant.

Mutative? Yes.

Example Select here to view the source code for the example.

 

Implementation Select here to view source code (Copyright (C) 1994, Hewlett-Packard Company) for this algorithm.



Kenny Zalewski
Mon May 13 04:03:40 EDT 1996