- random[meta header]
- std[meta namespace]
- class template[meta id-type]
- cpp11[meta cpp]
namespace std {
class bernoulli_distribution;
}
bernoulli_distribution
は、true
とfalse
という2つの値を確率分布させるクラスである。
このクラスは、ベルヌーイ分布(Bernoulli distribution)の実装である。
コンストラクタで設定された確率p
でtrue
が生成され、確率1.0 - p
でfalse
が生成される。
ベルヌーイ分布は、以下のような用途に使用できる:
- コイントス。コインの表が出るか裏が出るか
- 成功か失敗か
名前 |
説明 |
対応バージョン |
(constructor) |
コンストラクタ |
C++11 |
~bernoulli_distribution() = default; |
デストラクタ |
C++11 |
reset |
状態をリセットする |
C++11 |
名前 |
説明 |
対応バージョン |
p |
確率を取得する |
C++11 |
param |
分布のパラメータを取得/設定する |
C++11 |
min |
生成し得る値の下限を取得する |
C++11 |
max |
生成し得る値の上限を取得する |
C++11 |
型 |
説明 |
対応バージョン |
result_type |
乱数生成結果の型 bool 。 |
C++11 |
param_type |
分布パラメータの型。未規定。 |
C++11 |
#include <random>
#include <fstream>
int main()
{
std::random_device seed_gen;
std::default_random_engine engine(seed_gen());
// 確率0.7でtrueを生成し、確率0.3(1.0 - 0.7)でfalseを生成する
std::bernoulli_distribution dist(0.7);
std::ofstream result_file("bernoulli_distribution.tsv");
for (std::size_t n = 0; n < 1000; ++n) {
// 乱数を生成する
bool result = dist(engine);
result_file << result << "\t\n";
}
}
- std::bernoulli_distribution[color ff0000]
- seed_gen()[link random_device/op_call.md]
- std::ofstream[link /reference/fstream/basic_ofstream.md]
- dist(engine)[link bernoulli_distribution/op_call.md]
このプログラムによってある時に得られた結果(bernoulli_distribution.tsv)を図示する。
