- random[meta header]
- std[meta namespace]
- bernoulli_distribution[meta class]
- function[meta id-type]
- cpp11[meta cpp]
explicit bernoulli_distribution(double p = 0.5); // (1)
bernoulli_distribution() : bernoulli_distribution(0.5) {} // (1) C++20
explicit bernoulli_distribution(double p); // (2) C++20
explicit bernoulli_distribution(const param_type& parm); // (3)
- (1) : デフォルトコンストラクタ
- C++17まで : 確率
pを受け取るコンストラクタ。
- C++20 : 確率
pを0.5として(2)に委譲。
- (2) : 確率
pを受け取るコンストラクタ。確率pでtrueが生成され、確率1.0 - pでfalseが生成される。
- (3) : パラメータオブジェクトを受け取るコンストラクタ。
param_typeは、このクラスの(1)のコンストラクタと同じオーバーロードを持ち、それらのコンストラクタのパラメータを保持している。このコンストラクタでは、paramオブジェクトが持っているパラメータを、このクラスのコンストラクタに転送する。
- (1)(C++17まで)、(2) :
p >= 0.0 && p <= 1.0であること。
#include <iostream>
#include <random>
int main()
{
std::random_device seed_gen;
std::default_random_engine engine(seed_gen());
std::cout << std::boolalpha;
// 確率を指定する
{
// 確率0.7でtrueを生成し、確率0.3(1.0 - 0.7)でfalseを生成する
std::bernoulli_distribution dist(0.7);
for (int i = 0; i < 10; ++i) {
std::cout << dist(engine) << std::endl;
}
}
std::cout << std::endl;
// パラメータを通して範囲指定する
{
using dist_type = std::bernoulli_distribution;
// 確率0.7でtrueを生成し、確率0.3(1.0 - 0.7)でfalseを生成する
dist_type::param_type param(0.7);
dist_type dist(param);
for (int i = 0; i < 10; ++i) {
std::cout << dist(engine) << std::endl;
}
}
}
true
false
true
false
true
true
false
true
true
true
true
true
true
true
false
true
true
false
true
true