名前空間
変種
操作

std::binomial_distribution

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

成功確率pの独立な「はい」か「いいえ」かの試行(各成功確率p)を繰り返したときに、ちょうどk回の成功が発生する前に起こる失敗の回数iを生成します。

P(i|t,p) =

t
i


· pi
· (1 − p)t−i

得られる値は、各試行が確率 p で成功するような t 回の Yes/No 実験における成功回数です。

std::binomial_distributionRandomNumberDistribution を満たします。

目次

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

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]
特性
(C++11)
分布パラメータを返します。
(public member function) [edit]
(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]
疑似乱数分布のストリーム入出力を実行
(関数テンプレート) [編集]

[編集]

各試行の成功確率がちょうど 0.5 である二項分布のプロット。パスカルの三角形との関係を示しています(この場合、4回の試行で成功しない、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." MathWorld — A Wolfram Web Resource より。
English 日本語 中文(简体) 中文(繁體)