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) | 代入演算子は削除されています (パブリックメンバ関数) |
生成 | |
エンジンの状態を進めて生成された値を返します (パブリックメンバ関数) | |
特性 | |
非決定的な乱数ジェネレータのエントロピーの見積りを取得します (パブリックメンバ関数) | |
[静的] | 出力範囲の有り得る最小値を取得します (パブリック静的メンバ関数) |
[静的] | 出力範囲の有り得る最大値を取得します (パブリック静的メンバ関数) |
[編集]ノート
std::random_device
が非決定的でない有名な例は古いバージョンの MinGW です (bug 338、 GCC 9.2 で修正されました) が、 mingw-std-random_device のような代替実装もあります。 最新バージョンの MinGW は GCC with the MCF thread model からダウンロードできます。
[編集]例
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)];// 注: デモ専用です。// random_device の多くの実装はいったんエントロピープールを// 使い果たすと急激に性能が低下します。 実用的な用途のためには// 一般的には mt19937 のような PRNG をシードするためにのみ// random_device を使用します。}for(auto p : hist){std::cout<< p.first<<" : "<<std::string(p.second/100, '*')<<'\n';}}
出力例:
0 : ******************** 1 : ******************* 2 : ******************** 3 : ******************** 4 : ******************** 5 : ******************* 6 : ******************** 7 : ******************** 8 : ******************* 9 : ********************