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
がランダムバイトのソースを指定するものと想定されています。トークンの値として可能なものは、
"default"
、
"hw"
、
"rand_s"
、
"rdseed"
、
"rdrand"
、
"rdrnd"
、
"/dev/urandom"
、
"/dev/random"
、
"mt19937"
、およびmt19937エンジンのシードを指定する整数文字列です。(
"default"
以外のトークン値は特定のターゲットでのみ有効です。)
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++標準に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| P0935R0 | C++11 | default constructor was explicit | made implicit |