std::random_device:: random_device
| 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 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
|
random_device::random_device
|
||||
| Generation | ||||
| Characteristics | ||||
|
random_device
(
)
:
random_device
(
/*implementation-defined*/
)
{
}
|
(1) | (自 C++11 起) |
|
explicit
random_device
(
const
std::
string
&
token
)
;
|
(2) | (自 C++11 起) |
|
random_device
(
const
random_device
&
)
=
delete
;
|
(3) | (自 C++11 起) |
std::random_device
不可复制亦不可移动。
目录 |
异常
在失败时抛出从 std::exception 派生的实现定义异常。
注释
libstdc++
中的实现期望
token
用于指定随机字节的源。可能的 token 值包括
"default"
、
"hw"
、
"rand_s"
、
"rdseed"
、
"rdrand"
、
"rdrnd"
、
"/dev/urandom"
、
"/dev/random"
、
"mt19937"
,以及用于指定 mt19937 引擎种子的整数字符串。(除
"default"
外的 token 值仅对特定目标平台有效。)
在
libc++
的实现中,当配置为使用字符设备作为随机源时,期望
token
为字符设备名称,读取该设备可生成随机数;否则期望
token
为
"/dev/urandom"
。
当提供不受支持的令牌时,libstdc++ 和 libc++ 都会抛出异常。 Microsoft 的标准库 则会完全忽略该令牌。
示例
演示 Linux 系统上
std::random_device
常用的可用类型。
#include <iostream> #include <random> void demo(std::random_device&& rd) { static std::uniform_int_distribution<int> d(0, 9); for (int n = 0; n != 10; ++n) std::cout << d(rd) << ' '; std::cout << '\n'; } int main() { // 注意:提供的令牌如何处理是由实现定义的! // random_device 的默认令牌在 Linux 上通常是 /dev/urandom demo(std::random_device {}); // 请求 /dev/random,当熵为空时会阻塞 // 在 libstdc++ 中有效,在 msvc++ 中被忽略,在 libc++ 中可能抛出异常(截至 2022 年 11 月) demo(std::random_device {"/dev/random"}); // 请求非阻塞的 /dev/urandom,确保不使用 RDRAND // 在 libstdc++ 和 libc++ 中有效,在 msvc++ 中被忽略(截至 2022 年 11 月) demo(std::random_device {"/dev/urandom"}); // 请求 "hw",将使用基于硬件的随机生成(如 rdrand) // 在 libstdc++ 中有效,在 msvc++ 中被忽略,在 libc++ 中抛出异常(截至 2022 年 11 月) demo(std::random_device {"hw"}); }
可能的输出:
9 5 2 7 5 9 4 1 0 7 4 7 6 5 1 5 5 1 8 6 3 3 6 1 4 1 4 1 0 2 4 6 3 9 1 9 4 0 9 3
缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 适用范围 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| P0935R0 | C++11 | 默认构造函数为显式 | 改为隐式 |