std:: discrete_distribution
|
ヘッダーで定義
<random>
|
||
|
template
<
class
IntType
=
int
>
class discrete_distribution ; |
(C++11以降) | |
std::discrete_distribution
は区間
[
0
,
n
)
のランダムな整数を生成します。ここで個々の整数
i
の確率は
w
i
/S
として定義されます。これは
重み
の
i
番目の整数を、すべての
n
個の重みの合計で割った値です。
std::discrete_distribution
は
RandomNumberDistribution
のすべての要件を満たします。
目次 |
テンプレートパラメータ
| IntType | - | ジェネレータによって生成される結果の型。これが short 、 int 、 long 、 long long 、 unsigned short 、 unsigned int 、 unsigned long 、または unsigned long long のいずれでもない場合、動作は未定義です。 |
メンバー型
| メンバー型 | 定義 |
result_type
(C++11)
|
IntType |
param_type
(C++11)
|
パラメータセットの型。 RandomNumberDistribution を参照。 |
メンバー関数
|
(C++11)
|
新しい分布を構築する
(公開メンバ関数) |
|
(C++11)
|
分布の内部状態をリセットする
(公開メンバ関数) |
生成 |
|
|
(C++11)
|
分布内の次の乱数を生成する
(公開メンバ関数) |
特性 |
|
|
確率のリストを取得する
(公開メンバ関数) |
|
|
(C++11)
|
分布パラメータオブジェクトを取得または設定する
(公開メンバ関数) |
|
(C++11)
|
生成される可能性のある最小値を返す
(公開メンバ関数) |
|
(C++11)
|
生成される可能性のある最大値を返す
(公開メンバ関数) |
非メンバー関数
|
(C++11)
(C++11)
(C++20で削除)
|
二つの分布オブジェクトを比較する
(関数) |
|
(C++11)
|
擬似乱数分布に対するストリーム入出力を実行する
(関数テンプレート) |
例
#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