std:: binomial_distribution
From cppreference.net
|
ヘッダーで定義
<random>
|
||
|
template
<
class
IntType
=
int
>
class binomial_distribution ; |
(C++11以降) | |
ランダムな非負整数値 i を生成し、離散確率関数に従って分布させます:
-
P(i|t,p) =
⎛
⎜
⎝ t
i ⎞
⎟
⎠ · p i
· (1 − p) t−i
得られる値は、一連の t 回の「はい/いいえ」実験における成功回数であり、各実験は確率 p で成功します。
std::binomial_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)
|
新しい分布を構築する
(public member function) |
|
(C++11)
|
分布の内部状態をリセットする
(public member function) |
生成 |
|
|
(C++11)
|
分布内の次の乱数を生成する
(public member function) |
特性 |
|
|
(C++11)
|
分布パラメータを返す
(public member function) |
|
(C++11)
|
分布パラメータオブジェクトを取得または設定する
(public member function) |
|
(C++11)
|
生成され得る最小値を返す
(public member function) |
|
(C++11)
|
生成され得る最大値を返す
(public member function) |
非メンバー関数
|
(C++11)
(C++11)
(removed in C++20)
|
二つの分布オブジェクトを比較する
(関数) |
|
(C++11)
|
擬似乱数分布に対するストリーム入出力を実行する
(関数テンプレート) |
例
各試行の成功確率が正確に0.5の二項分布のプロット。パスカルの三角形との関係を示している(この場合、4回の試行のうち0回、1回、2回、3回、4回すべてが成功する確率は1:4:6:4:1)。
このコードを実行
#include <iomanip> #include <iostream> #include <map> #include <random> #include <string> int main() { std::random_device rd; std::mt19937 gen(rd()); // perform 4 trials, each succeeds 1 in 2 times std::binomial_distribution<> d(4, 0.5); std::map<int, int> hist; for (int n = 0; n != 10000; ++n) ++hist[d(gen)]; for (auto const& [x, y] : hist) std::cout << x << ' ' << std::string(y / 100, '*') << '\n'; }
出力例:
0 ****** 1 ************************ 2 ************************************* 3 ************************* 4 ******
外部リンク
| Weisstein, Eric W. "Binomial Distribution." From MathWorld — A Wolfram Web Resource. |
| Weisstein, Eric W. "二項分布" MathWorld — Wolfram Webリソースより |