C++ 参考手册
- C++11
 - C++14
 - C++17
 - C++20
 - C++ 编译器支持情况表
 - 独立与宿主实现
 - C++ 语言
 - C++ 关键词
 - 预处理器
 - C++ 标准库头文件
 - 具名要求
 - 功能特性测试 (C++20)
 - 工具库
 - 类型支持(基本类型、RTTI、类型特性)
 - 概念库 (C++20)
 - 错误处理
 - 动态内存管理
 - 日期和时间工具
 - 字符串库
 - 容器库
 - 迭代器库
 - 范围库 (C++20)
 - 算法库
 - std::execution::sequenced_policy, std::execution::parallel_policy, std::execution::parallel_unsequenced_policy, std::execution::unsequenced_policy
 - 有制约算法 (C++20 起)
 - std::is_execution_policy
 - std::execution::seq, std::execution::par, std::execution::par_unseq, std::execution::unseq
 - std::all_of, std::any_of, std::none_of
 - std::for_each_n
 - std::sort
 - std::reduce
 - std::inclusive_scan
 - std::exclusive_scan
 - std::random_shuffle, std::shuffle
 - std::clamp
 - std::equal
 - std::is_permutation
 - std::mismatch
 - std::swap
 - std::search
 - std::transform
 - std::for_each
 - std::make_heap
 - std::count, std::count_if
 - std::adjacent_find
 - std::find, std::find_if, std::find_if_not
 - std::find_end
 - std::find_first_of
 - std::search_n
 - std::lexicographical_compare
 - std::lexicographical_compare_three_way
 - std::copy, std::copy_if
 - std::copy_n
 - std::copy_backward
 - std::move
 - std::move_backward
 - std::shift_left, std::shift_right
 - std::fill
 - std::fill_n
 - std::generate
 - std::generate_n
 - std::iter_swap
 - std::swap_ranges
 - std::sample
 - std::remove, std::remove_if
 - std::replace, std::replace_if
 - std::reverse
 - std::rotate
 - std::unique
 - std::remove_copy, std::remove_copy_if
 - std::replace_copy, std::replace_copy_if
 - std::reverse_copy
 - std::rotate_copy
 - std::unique_copy
 - std::is_partitioned
 - std::partition_point
 - std::partition
 - std::partition_copy
 - std::stable_partition
 - std::is_sorted
 - std::is_sorted_until
 - std::stable_sort
 - std::partial_sort
 - std::partial_sort_copy
 - std::nth_element
 - std::lower_bound
 - std::upper_bound
 - std::binary_search
 - std::equal_range
 - std::merge
 - std::inplace_merge
 - std::set_difference
 - std::set_intersection
 - std::set_symmetric_difference
 - std::set_union
 - std::includes
 - std::is_heap
 - std::is_heap_until
 - std::sort_heap
 - std::push_heap
 - std::pop_heap
 - std::max
 - std::max_element
 - std::min
 - std::min_element
 - std::minmax
 - std::minmax_element
 - std::next_permutation
 - std::prev_permutation
 - std::iota
 - std::inner_product
 - std::adjacent_difference
 - std::accumulate
 - std::transform_reduce
 - std::partial_sum
 - std::transform_inclusive_scan
 - std::transform_exclusive_scan
 - std::qsort
 - std::bsearch
 - 注释
 - 数值库
 - 输入/输出库
 - 文件系统库
 - 本地化库
 - 正则表达式库
 - 原子操作库
 - 线程支持库
 - 实验性 C++ 特性
 - 有用的资源
 - 索引
 - std 符号索引
 - 协程支持 (C++20)
 - C++ 关键词
 
std::random_shuffle, std::shuffle
|   定义于头文件  <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 。3) 随机数生成器为函数对象 
g 。参数
| first, last | - | 要随机打乱的元素范围 | 
| r | - |  函数对象。若以 r(n) 调用,则返回可转换为 std::iterator_traits<RandomIt>::difference_type 的在区间 [0,n) 中随机选取的值
 | 
| g | - | 均匀随机位生成器 (UniformRandomBitGenerator) ,其结果类型可隐式转换为 std::iterator_traits<RandomIt>::difference_type | 
| 类型要求 | ||
 -RandomIt 必须满足值可交换 (ValueSwappable)  和 遗留随机访问迭代器 (LegacyRandomAccessIterator)  的要求。
 | ||
 -std::remove_reference_t<URBG> 必须满足均匀随机位生成器 (UniformRandomBitGenerator)  的要求。
 | ||
返回值
(无)
复杂度
与 first 和 last 间的距离成线性。
注意
标准未钦定实现,故即使你用准确相同的 RandomFunc 或 URBG ,也可能通过不同的标准库实现得到不同结果。
可能的实现
| 版本一 | 
|---|
template< class RandomIt > void random_shuffle( RandomIt first, RandomIt last ) { 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[std::rand() % (i+1)]); // rand() % (i+1) 实际上不准确,因为生成的数对于多数 i 值不均匀分布。 // 正确实现将实际上需要重新实现 C++11 std::uniform_distributtion , // 这超出了此示例的范畴。 } }  | 
| 版本二 | 
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)]); } }  | 
| 版本三 | 
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; 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))]); } }  | 
示例
下列代码随机打乱整数 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); 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
参阅
|    产生某个元素范围的按字典顺序的下一个较大的排列   (函数模板)  | |
|    产生某个元素范围的按字典顺序的下一个较小的排列   (函数模板)  |