std:: random_device
From cppreference.net
C++
Numerics library
| Common mathematical functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Mathematical special functions (C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Mathematical constants (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Basic linear algebra algorithms (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Data-parallel types (SIMD) (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Floating-point environment (C++11) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Complex numbers | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Numeric array (
valarray
)
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Pseudo-random number generation | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Bit manipulation (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Saturation arithmetic (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Factor operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Interpolations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Generic numeric operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| C-style checked integer arithmetic | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Pseudo-random number generation
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::random_device
| Member functions | ||||
| Generation | ||||
| Characteristics | ||||
|
定义于头文件
<random>
|
||
|
class
random_device
;
|
(C++11 起) | |
std::random_device
是一个均匀分布的整数随机数生成器,可生成非确定性随机数。
若实现无法使用非确定性源(如硬件设备),
std::random_device
可能基于实现定义的伪随机数引擎实现。这种情况下,每个
std::random_device
对象可能生成相同的数字序列。
目录 |
成员类型
| 成员类型 | 定义 |
result_type
(C++11)
|
unsigned int |
成员函数
构造 |
|
|
构造引擎
(公开成员函数) |
|
|
operator=
(deleted)
(C++11)
|
赋值运算符被删除
(公开成员函数) |
生成 |
|
|
推进引擎状态并返回生成的值
(公开成员函数) |
|
特性 |
|
|
(C++11)
|
获取非确定性随机数生成器的熵估计值
(公开成员函数) |
|
[static]
|
获取输出范围中的最小可能值
(公开静态成员函数) |
|
[static]
|
获取输出范围中的最大可能值
(公开静态成员函数) |
注释
一个值得注意的实现是旧版本MinGW-w64中的
std::random_device
具有确定性(
缺陷报告338
,自GCC 9.2起已修复)。最新版MinGW-w64可从
采用MCF线程模型的GCC
获取。
示例
运行此代码
#include <iostream> #include <map> #include <random> #include <string> int main() { std::random_device rd; std::map<int, int> hist; std::uniform_int_distribution<int> dist(0, 9); for (int n = 0; n != 20000; ++n) ++hist[dist(rd)]; // 注意:仅用于演示:许多实现中的 random_device 性能会 // 在熵池耗尽后急剧下降。实际应用中 // random_device 通常仅用于初始化 // 伪随机数生成器(如 mt19937) for (auto [x, y] : hist) std::cout << x << " : " << std::string(y / 100, '*') << '\n'; }
可能的输出:
0 : ******************** 1 : ******************* 2 : ******************** 3 : ******************** 4 : ******************** 5 : ******************* 6 : ******************** 7 : ******************** 8 : ******************* 9 : ********************