名前空間
変種
操作

std::discrete_distribution

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

std::discrete_distribution は、区間 [0n) 上のランダムな整数を生成します。ここで、各整数 i が生成される確率は、i 番目の重み wi を全 n 個の重みの合計 S で割った値、つまり wi/S で定義されます。

std::discrete_distribution は、RandomNumberDistribution のすべての要件を満たします。

目次

[編集] テンプレートパラメータ

IntType - ジェネレータによって生成される結果の型。これが shortintlonglong longunsigned shortunsigned intunsigned long、または unsigned long long のいずれでもない場合、その効果は未定義です。

[編集] メンバ型

メンバ型 定義
result_type (C++11) IntType
param_type (C++11) パラメータセットの型。RandomNumberDistribution を参照してください。

[編集] メンバ関数

新しい分布を構築します。
(public member function) [edit]
(C++11)
分布の内部状態をリセットします。
(public member function) [edit]
生成
分布における次の乱数を生成する
(public member function) [edit]
特性
確率のリストを取得します。
(public member function) [編集]
(C++11)
分布パラメータオブジェクトを取得または設定します。
(public member function) [edit]
(C++11)
生成される可能性のある最小値を返します。
(public member function) [edit]
(C++11)
生成される可能性のある最大値を返します。
(public member function) [edit]

[編集] 非メンバ関数

(C++11)(C++11)(C++20で削除)
2つの分布オブジェクトを比較します。
(function) [edit]
疑似乱数分布のストリーム入出力を実行
(関数テンプレート) [編集]

[編集]

#include <iomanip>
#include <iostream>
#include <map>
#include <random>
 
int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    std::discrete_distribution<> d({40, 10, 10, 40});
    std::map<int, int> map;
 
    for (int n = 0; n < 1e4; ++n)
        ++map[d(gen)];
 
    for (const auto& [num, count] : map)
        std::cout << num << " generated " << std::setw(4) << count << " times\n";
}

実行結果の例

0 generated 4037 times
1 generated  962 times
2 generated 1030 times
3 generated 3971 times
English 日本語 中文(简体) 中文(繁體)