std::random_device
提供: cppreference.com
ヘッダ <random> で定義 | ||
class random_device; | (C++11およびそれ以降) | |
std::random_device
は非決定的な乱数を生成する一様分布した整数の乱数ジェネレータです。
処理系が非決定的な生成源 (ハードウェアデバイスなど) を利用できない場合、 std::random_device
は処理系定義の擬似乱数エンジンを用いて実装されることがあります。 この場合、各 std::random_device
オブジェクトは同じ数値列を生成するかもしれません。
目次 |
[編集]メンバ型
メンバ型 | 定義 |
result_type | unsignedint |
[編集]メンバ関数
構築 | |
エンジンを構築します (パブリックメンバ関数) | |
operator= (deleted) | 代入演算子は削除されています (パブリックメンバ関数) |
生成 | |
エンジンの状態を進めて生成された値を返します (パブリックメンバ関数) | |
特性 | |
非決定的な乱数ジェネレータのエントロピーの見積りを取得します (パブリックメンバ関数) | |
[static] | 出力範囲の有り得る最小値を取得します (パブリック静的メンバ関数) |
[static] | 出力範囲の有り得る最大値を取得します (パブリック静的メンバ関数) |
[編集]ノート
std::random_device
が非決定的でない有名な例は MinGW です (bug 338)。 コミュニティによる MinGW 用の random_device サポートの実装があります (mingw-std-random_device)。
[編集]例
Run this code
#include <iostream>#include <string>#include <map>#include <random> 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)];// note: demo only: the performance of many // implementations of random_device degrades sharply// once the entropy pool is exhausted. For practical use// random_device is generally only used to seed // a PRNG such as mt19937}for(auto p : hist){std::cout<< p.first<<" : "<<std::string(p.second/100, '*')<<'\n';}}
出力例:
0 : ******************** 1 : ******************* 2 : ******************** 3 : ******************** 4 : ******************** 5 : ******************* 6 : ******************** 7 : ******************** 8 : ******************* 9 : ********************