Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/random shuffle"

From cppreference.com
< cpp‎ | algorithm
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}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list header | algorithm}}
+
{{dcl header | algorithm}}
{{ddcl list item | num=1 |
+
{{dcl | num=1 |
 
template< class RandomIt >
 
template< class RandomIt >
 
void random_shuffle( RandomIt first, RandomIt last );
 
void random_shuffle( RandomIt first, RandomIt last );
 
}}
 
}}
{{ddcl list item | num=2 | notes={{mark until c++11}}<br><br><br><br>{{mark since c++11}} |
+
{{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 );
 
}}
 
}}
{{ddcl list item | num=3 | notes={{mark since c++11}} |
+
{{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 );
 
}}
 
}}
{{ddcl list end}}
+
{{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 {{c|std::rand}} is often used.
+
@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===
{{param list begin}}
+
{{par begin}}
{{param list item | first, last | the range of elements to shuffle randomly}}
+
{{par | first, last | the range of elements to shuffle randomly}}
{{param list item | 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 | 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)}} }}
{{param list item | 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 | 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>}}) }}
{{param list hreq}}
+
{{par hreq}}
{{param list req concept | RandomIt | RandomAccessIterator | ValueSwappable}}
+
{{par req concept | RandomIt | RandomAccessIterator | ValueSwappable}}
{{param list req concept | URNG | UniformRandomNumberGenerator}}
+
{{par req concept | URNG | UniformRandomNumberGenerator}}
{{param list end}}
+
{{par end}}
  
 
===Return value===
 
===Return value===
Line 108: Line 108:
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/algorithm/dcl list next_permutation}}
+
{{dsc inc | cpp/algorithm/dcl list next_permutation}}
{{dcl list template | cpp/algorithm/dcl list prev_permutation}}
+
{{dsc inc | cpp/algorithm/dcl list prev_permutation}}
{{dcl list end}}
+
{{dsc end}}
  
 
[[de:cpp/algorithm/random shuffle]]
 
[[de:cpp/algorithm/random shuffle]]

Revision as of 17:48, 31 May 2013

 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
(C++11)                (C++11)(C++11)

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
random_shuffleshuffle
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
 
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 );

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

Template:par req concept Template:par req concept

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:

#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

See also

Template:cpp/algorithm/dcl list next permutationTemplate:cpp/algorithm/dcl list prev permutation