最終更新日時(UTC):
が更新

履歴 編集

function template
<random>

std::bernoulli_distribution::operator()(C++11)

template <class URBG>
result_type operator()(URBG& g);                         // (1)

template <class URBG>
result_type operator()(URBG& g, const param_type& parm); // (2)

概要

  • (1) : コンストラクタで指定されたパラメータに基いて、乱数生成を行う
  • (2) : コンストラクタで設定されたパラメータの代わりに、paramを乱数生成のパラメータとして使用して乱数生成を行う

戻り値

指定された確率に基いて、ランダムな値を生成して返す。
確率p()trueを生成し、確率1 - p()falseを生成する。

計算量

償却定数時間(g()の呼び出し回数)

#include <iostream>
#include <random>

int main()
{
  std::random_device seed_gen;
  std::mt19937 engine(seed_gen());

  std::cout << std::boolalpha;

  // (1)
  {
    std::bernoulli_distribution dist(0.5);

    for (int i = 0; i < 5; ++i) {
      bool result = dist(engine);
      std::cout << result << std::endl;
    }
  }

  // (2) パラメータを渡すバージョン
  std::cout << std::endl;
  {
    using dist_type = std::bernoulli_distribution;
    dist_type dist;

    for (int i = 0; i < 5; ++i) {
      dist_type::param_type param(0.5);
      bool result = dist(engine, param);
      std::cout << result << std::endl;
    }
  }
}

出力例

false
false
false
true
true

true
false
false
true
true

バージョン

言語

  • C++11

処理系

参照