Namespaces
Variants

std:: random_shuffle, std:: shuffle

From cppreference.net
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
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
random_shuffle shuffle
(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
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
定义于头文件 <algorithm>
template < class RandomIt >
void random_shuffle ( RandomIt first, RandomIt last ) ;
(1) (C++14 中弃用)
(C++17 中移除)
(2)
template < class RandomIt, class RandomFunc >
void random_shuffle ( RandomIt first, RandomIt last, RandomFunc & r ) ;
(C++11 前)
template < class RandomIt, class RandomFunc >
void random_shuffle ( RandomIt first, RandomIt last, RandomFunc && r ) ;
(C++11 起)
(C++14 中弃用)
(C++17 中移除)
template < class RandomIt, class URBG >
void shuffle ( RandomIt first, RandomIt last, URBG && g ) ;
(3) (C++11 起)

对给定范围 [ first , last ) 内的元素进行重新排序,使得这些元素的每种可能排列都具有相等的出现概率。

1) 随机性来源是实现定义的,但函数 std::rand 通常被使用。
2) 随机性来源是函数对象 r
若满足以下任意条件,则行为未定义:
  • r 的返回类型不可转换为 std:: iterator_traits < RandomIt > :: difference_type
  • 对于类型为 std:: iterator_traits < RandomIt > :: difference_type 的正值 n r ( n ) 的结果不是区间 [ 0 , n ) 内的随机选择值。
3) 随机性来源是对象 g
给定类型 T std:: remove_reference_t < URBG > ,若满足以下任意条件,则行为未定义:
(C++20 前)

如果 * first 的类型不满足 Swappable (C++11 前) RandomIt 不满足 ValueSwappable (C++11 起) ,则行为未定义。

目录

参数

first, last - 定义待随机重排元素 范围 的迭代器对
r - 返回随机选定值的函数对象
g - 返回随机选定值的生成器对象
类型要求
-
RandomIt 必须满足 LegacyRandomAccessIterator 的要求。

复杂度

恰好需要 std:: distance ( first, last ) - 1 次交换。

可能的实现

另请参阅 libstdc++ libc++ 中的实现。

random_shuffle (1)
template<class RandomIt>
void random_shuffle(RandomIt first, RandomIt last)
{
    typedef typename std::iterator_traits<RandomIt>::difference_type diff_t;
    for (diff_t i = last - first - 1; i > 0; --i)
    {
        using std::swap;
        swap(first[i], first[std::rand() % (i + 1)]);
        // rand() % (i + 1) 实际上并不正确,因为对于大多数 i 值,生成的数字
        // 并非均匀分布。正确的代码应该是 C++11 std::uniform_int_distribution 实现的变体。
    }
}
random_shuffle (2)
template<class RandomIt, class RandomFunc>
void random_shuffle(RandomIt first, RandomIt last, RandomFunc&& r)
{
    typedef typename std::iterator_traits<RandomIt>::difference_type diff_t;
    for (diff_t i = last - first - 1; i > 0; --i)
    {
        using std::swap;
        swap(first[i], first[r(i + 1)]);
    }
}
shuffle (3)
template<class RandomIt, class URBG>
void shuffle(RandomIt first, RandomIt last, URBG&& g)
{
    typedef typename std::iterator_traits<RandomIt>::difference_type diff_t;
    typedef std::uniform_int_distribution<diff_t> distr_t;
    typedef typename distr_t::param_type param_t;
    distr_t D;
    for (diff_t i = last - first - 1; i > 0; --i)
    {
        using std::swap;
        swap(first[i], first[D(g, param_t(0, i))]);
    }
}

注释

请注意,具体实现方式并非由标准强制规定,因此即使您使用完全相同的 RandomFunc URBG (均匀随机数生成器),在不同的标准库实现中仍可能获得不同结果。

C++17 中移除 std::random_shuffle 的原因在于,仅迭代器版本通常依赖于 std::rand ,而该函数现已被提议弃用。( std::rand 应当被 <random> 头文件中的类替代,因为 std::rand 被认为存在缺陷。)此外,仅迭代器版本的 std::random_shuffle 通常依赖于全局状态。而 std::shuffle 的洗牌算法是更推荐的替代方案,因为它以 URBG 作为其第三个参数。

示例

随机打乱整数序列 [ 1 , 10 ]

#include <algorithm>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>
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);
    std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
}

可能的输出:

8 6 10 4 2 3 7 1 9 5

缺陷报告

下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

缺陷报告 适用标准 发布时行为 正确行为
LWG 395 C++98 未指定重载 (1) 的随机性来源,且
由于C库要求 std::rand 不能作为来源
由实现定义,
允许使用 std::rand
LWG 552
( N2423 )
C++98 未要求 r 必须是重载
(2) 的随机性来源 [1]
必须要求
  1. 重载 (3) 存在相同缺陷,但该部分决议不适用于 C++98。

参见

生成一个元素范围的下一个更大字典序排列
(函数模板)
生成一个元素范围的下一个更小字典序排列
(函数模板)
随机重排范围中的元素
(算法函数对象)