名前空間
変種
操作

std::bernoulli_distribution

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

離散確率関数に従って、ランダムなブール値を生成します。true が生成される確率は次のようになります。

P(b|p) =

p, if b is true
1 − p, if b is false

std::bernoulli_distributionRandomNumberDistribution を満たします。

目次

[編集] メンバー型

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

[編集] メンバー関数

新しい分布を構築します。
(public member function) [edit]
(C++11)
分布の内部状態をリセットします。
(public member function) [edit]
生成
分布における次の乱数を生成する
(public member function) [edit]
特性
(C++11)
p 分布パラメータ(true を生成する確率)を返します。
(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>
#include <string>
 
int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    // give "true" 1/4 of the time
    // give "false" 3/4 of the time
    std::bernoulli_distribution d(0.25);
 
    std::map<bool, int> hist;
    for (int n = 0; n < 10000; ++n)
        ++hist[d(gen)];
 
    std::cout << std::boolalpha;
    for (auto const& [key, value] : hist)
        std::cout << std::setw(5) << key << ' '
                  << std::string(value / 500, '*') << '\n';
}

実行結果の例

false ***************
 true ****

[編集] 外部リンク

Weisstein, Eric W. "Bernoulli Distribution." MathWorld — A Wolfram Web Resourceより。
English 日本語 中文(简体) 中文(繁體)