Difference between revisions of "cpp/algorithm/random shuffle"
From cppreference.com
m (r2.7.3) (Robot: Adding de, es, fr, it, ja, pt, zh) |
m (Shorten template names. Use {{lc}} where appropriate.) |
||
Line 1: | Line 1: | ||
{{cpp/title|random_shuffle|shuffle}} | {{cpp/title|random_shuffle|shuffle}} | ||
{{cpp/algorithm/navbar}} | {{cpp/algorithm/navbar}} | ||
− | {{ | + | {{dcl begin}} |
− | {{ | + | {{dcl header | algorithm}} |
− | {{ | + | {{dcl | num=1 | |
template< class RandomIt > | template< class RandomIt > | ||
void random_shuffle( RandomIt first, RandomIt last ); | void random_shuffle( RandomIt first, RandomIt last ); | ||
}} | }} | ||
− | {{ | + | {{dcl | num=2 | notes={{mark until c++11}}<br><br><br><br>{{mark since c++11}} | |
template< class RandomIt, class RandomFunc > | template< class RandomIt, class RandomFunc > | ||
void random_shuffle( RandomIt first, RandomIt last, RandomFunc& r ); | void random_shuffle( RandomIt first, RandomIt last, RandomFunc& r ); | ||
Line 14: | Line 14: | ||
void random_shuffle( RandomIt first, RandomIt last, RandomFunc&& r ); | void random_shuffle( RandomIt first, RandomIt last, RandomFunc&& r ); | ||
}} | }} | ||
− | {{ | + | {{dcl | num=3 | notes={{mark since c++11}} | |
template< class RandomIt, class URNG > | template< class RandomIt, class URNG > | ||
void shuffle( RandomIt first, RandomIt last, URNG&& g ); | void shuffle( RandomIt first, RandomIt last, URNG&& g ); | ||
}} | }} | ||
− | {{ | + | {{dcl end}} |
Reorders the elements in the given range {{tt|[first, last)}} such that each possible permutation of those elements has equal probability of appearance. | Reorders the elements in the given range {{tt|[first, last)}} such that each possible permutation of those elements has equal probability of appearance. | ||
− | @1@ The random number generator is implementation-defined, but the function {{ | + | @1@ The random number generator is implementation-defined, but the function {{lc|std::rand}} is often used. |
@2@ The random number generator is the function object {{tt|r}}. | @2@ The random number generator is the function object {{tt|r}}. | ||
Line 29: | Line 29: | ||
===Parameters=== | ===Parameters=== | ||
− | {{ | + | {{par begin}} |
− | {{ | + | {{par | first, last | the range of elements to shuffle randomly}} |
− | {{ | + | {{par | r | function object returning a randomly chosen value of type convertible to {{tt|iterator_traits<RandomIt>::difference_type}} in the interval [0,n) if invoked as {{tt|r(n)}} }} |
− | {{ | + | {{par | g | function object returning a randomly chosen value of type {{tt|URNG::result_type}} in the interval [g.min(), g.max()] if invoked as {{tt|g()}} (e.g. any of the uniform random number generators from {{tt|<random>}}) }} |
− | {{ | + | {{par hreq}} |
− | {{ | + | {{par req concept | RandomIt | RandomAccessIterator | ValueSwappable}} |
− | {{ | + | {{par req concept | URNG | UniformRandomNumberGenerator}} |
− | {{ | + | {{par end}} |
===Return value=== | ===Return value=== | ||
Line 108: | Line 108: | ||
===See also=== | ===See also=== | ||
− | {{ | + | {{dsc begin}} |
− | {{ | + | {{dsc inc | cpp/algorithm/dcl list next_permutation}} |
− | {{ | + | {{dsc inc | cpp/algorithm/dcl list prev_permutation}} |
− | {{ | + | {{dsc end}} |
[[de:cpp/algorithm/random shuffle]] | [[de:cpp/algorithm/random shuffle]] |
Revision as of 17:48, 31 May 2013
Defined in header <algorithm>
|
||
template< class RandomIt > void random_shuffle( RandomIt first, RandomIt last ); |
(1) | |
template< class RandomIt, class RandomFunc > void random_shuffle( RandomIt first, RandomIt last, RandomFunc& r ); |
(2) | (until C++11) (since C++11) |
template< class RandomIt, class URNG > void shuffle( RandomIt first, RandomIt last, URNG&& g ); |
(3) | (since C++11) |
Reorders the elements in the given range [first, last)
such that each possible permutation of those elements has equal probability of appearance.
1) The random number generator is implementation-defined, but the function std::rand is often used.
2) The random number generator is the function object
r
. 3) The random number generator is the function object
g
.Contents |
Parameters
first, last | - | the range of elements to shuffle randomly |
r | - | function object returning a randomly chosen value of type convertible to iterator_traits<RandomIt>::difference_type in the interval [0,n) if invoked as r(n)
|
g | - | function object returning a randomly chosen value of type URNG::result_type in the interval [g.min(), g.max()] if invoked as g() (e.g. any of the uniform random number generators from <random> )
|
Type requirements |
Return value
(none)
Complexity
linear in the distance between first
and last
Possible implementation
First version |
---|
template<class RandomIt, class RandomFunc> void random_shuffle(RandomIt first, RandomIt last, RandomFunc&& r) { typename std::iterator_traits<RandomIt>::difference_type i, n; n = last - first; for (i = n-1; i > 0; --i) { using std::swap; swap(first[i], first[r(i+1)]); } } |
Second version |
template<class RandomIt, class UniformRandomNumberGenerator> void shuffle(RandomIt first, RandomIt last, UniformRandomNumberGenerator&& g) { typedef typename std::iterator_traits<RandomIt>::difference_type diff_t; typedef typename std::make_unsigned<diff_t>::type udiff_t; typedef typename std::uniform_int_distribution<udiff_t> distr_t; typedef typename distr_t::param_type param_t; distr_t D; diff_t n = last - first; for (diff_t i = n-1; i > 0; --i) { using std::swap; swap(first[i], first[D(g, param_t(0, i))]); } } |
Example
The following code randomly shuffles the integers 1..10:
Run this code
#include <random> #include <algorithm> #include <iterator> #include <iostream> int main() { std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; std::random_device rd; std::mt19937 g(rd()); std::shuffle(v.begin(), v.end(), g); copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << "\n"; }
Possible output:
8 6 10 4 2 3 7 1 9 5