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

履歴 編集

function template
<random>

std::negative_binomial_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を乱数生成のパラメータとして使用して乱数生成を行う

戻り値

指定された成功確率と成功回数に基いて、k回成功するまでに失敗した回数を返す。

計算量

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

#include <iostream>
#include <random>

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

  // (1)
  {
    // 確率0.5で成功する事象を3回成功させる
    std::negative_binomial_distribution<> dist(3, 0.5);

    // 3回成功するまでに失敗した回数を取得
    int result = dist(engine);
    std::cout << result << std::endl;
  }

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

    // 確率0.5で成功する事象を3回成功させる
    dist_type::param_type param(3, 0.5);

    // 3回成功するまでに失敗した回数を取得
    int result = dist(engine, param);
    std::cout << result << std::endl;
  }
}

出力例

5
1

バージョン

言語

  • C++11

処理系

備考

GCC 4.8時点のlibstdc++では、パラメータを渡すバージョンのoperator()呼び出しはコンパイルエラーになる。
Bug 58302 - compilation error : std::negative_binomial_distribution::operator(e, p)

参照