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
が決定的動作を示す注目すべき実装例(
bug 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のようなPRNGをシードするためだけに使用されます for (auto [x, y] : hist) std::cout << x << " : " << std::string(y / 100, '*') << '\n'; }
出力例:
0 : ******************** 1 : ******************* 2 : ******************** 3 : ******************** 4 : ******************** 5 : ******************* 6 : ******************** 7 : ******************** 8 : ******************* 9 : ********************