名前空間
変種
操作

std::random_device

From cppreference.com
< cpp‎ | numeric‎ | random
 
 
 
 
 
ヘッダー <random> で定義
class random_device;
(C++11以降)

std::random_device は、非決定論的な乱数を生成する、一様分布の整数乱数生成器です。

std::random_device は、実装で非決定論的なソース(例:ハードウェアデバイス)が利用できない場合、実装定義の疑似乱数生成エンジンで実装されることがあります。この場合、各 std::random_device オブジェクトは同じ乱数列を生成する可能性があります。

目次

[編集] メンバー型

メンバ型 定義
result_type (C++11) unsigned int

[編集] メンバー関数

構築
エンジンを構築します。
(public member function) [編集]
operator=
(削除済み) (C++11)
代入演算子は削除されています。
(public member function)
生成
エンジンの状態を進め、生成された値を返す
(public member function) [編集]
特性
非決定論的乱数生成器のエントロピー推定値を取得します。
(public メンバー関数) [編集]
[static]
出力範囲の最小値を返します。
(public static member function) [編集]
[static]
出力範囲の最大値を返します。
(public static member function) [編集]

[編集] 注記

MinGW-w64 の古いバージョンで std::random_device が決定論的であるという注目すべき実装があります(バグ 338、GCC 9.2 以降で修正済み)。最新の MinGW-w64 バージョンは GCC with the MCF thread model からダウンロードできます。

[編集]

#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)]; // 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 [x, y] : hist)
        std::cout << x << " : " << std::string(y / 100, '*') << '\n';
}

実行結果の例

0 : ********************
1 : *******************
2 : ********************
3 : ********************
4 : ********************
5 : *******************
6 : ********************
7 : ********************
8 : *******************
9 : ********************
English 日本語 中文(简体) 中文(繁體)