std::discrete_distribution
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <random> で定義
|
||
template< class IntType = int > class discrete_distribution; |
(C++11以上) | |
std::discrete_distribution は、個々の整数 i の確率がそれぞれ w
i/S、つまり、 i 番目の整数の重みを n 個の重み全部の合計で割ったものとして定義される、区間 [0, n) のランダムな整数を生成します。
std::discrete_distribution は RandomNumberDistribution の要件をすべて満たします。
テンプレート引数
| IntType | - | ジェネレータが生成する結果の型。 short、 int、 long、 long long、 unsigned short、 unsigned int、 unsigned long または unsigned long long のいずれかでない場合、効果は未定義です
|
メンバ型
| メンバ型 | 定義 |
result_type
|
IntType
|
param_type
|
パラメータセットの型、 RandomNumberDistribution を参照してください |
メンバ関数
| 新しい分布を構築します (パブリックメンバ関数) | |
| 分布の内部状態をリセットします (パブリックメンバ関数) | |
生成 | |
| 分布の次の乱数を生成します (パブリックメンバ関数) | |
特性 | |
| 確率のリストを取得します (パブリックメンバ関数) | |
| 分布のパラメータオブジェクトを取得または設定します (パブリックメンバ関数) | |
| 生成される可能性のある最小値を返します (パブリックメンバ関数) | |
| 生成される可能性のある最大値を返します (パブリックメンバ関数) |
非メンバ関数
| 2つの分布オブジェクトを比較します (関数) | |
| 乱数分布に対してストリーム入出力を行います (関数テンプレート) |
例
Run this code
#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> m;
for(int n=0; n<10000; ++n) {
++m[d(gen)];
}
for(auto p : m) {
std::cout << p.first << " generated " << p.second << " times\n";
}
}
出力:
0 generated 4028 times
1 generated 978 times
2 generated 1012 times
3 generated 3982 times