Skip to content

Latest commit

 

History

History
89 lines (68 loc) · 2.37 KB

File metadata and controls

89 lines (68 loc) · 2.37 KB

operator()

  • random[meta header]
  • std[meta namespace]
  • geometric_distribution[meta class]
  • function template[meta id-type]
  • cpp11[meta cpp]
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を乱数生成のパラメータとして使用して乱数生成を行う

戻り値

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

計算量

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

#include <iostream>
#include <random>

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

  // (1)
  {
    // 確率0.5で成功する事象を、成功するまで試行する
    std::geometric_distribution<> dist(0.5);

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

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

    // 確率0.5で成功する事象を、成功するまで試行する
    dist_type::param_type param(0.5);

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

出力例

2
0

バージョン

言語

  • C++11

処理系

  • Clang: 3.0 [mark verified]
  • GCC: 4.7.2(パラメータを渡さないバージョンのみ) [mark verified]
  • ICC: ??
  • Visual C++: ??

備考

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

参照