Skip to content

Latest commit

 

History

History
95 lines (75 loc) · 2.06 KB

File metadata and controls

95 lines (75 loc) · 2.06 KB

operator()

  • random[meta header]
  • std[meta namespace]
  • uniform_real_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を乱数生成のパラメータとして使用して乱数生成を行う

戻り値

指定された値の範囲に基いて、等確率でランダムな値を生成して返す。
値の範囲は a <= i < b である。つまり、範囲の最小値を含み、最大値を含まない。

計算量

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

#include <iostream>
#include <random>

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

  // (1)
  {
    std::uniform_real_distribution<> dist(0.0, 1.0);

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

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

    for (int i = 0; i < 5; ++i) {
      dist_type::param_type param(0.0, 1.0);
      double result = dist(engine, param);
      std::cout << result << std::endl;
    }
  }
}
  • dist(engine)[color ff0000]
  • dist(engine, param)[color ff0000]

出力例

0.254291
0.567725
0.240681
0.164293
0.227867

0.576028
0.549507
0.901166
0.716245
0.272544

バージョン

言語

  • C++11

処理系

参照