<random>

公共成員函式
<random>

std::binomial_distribution::operator()

(1)
template<class URNG>result_type operator()(URNG& g);
(2)
template<class URNG>result_type operator()(URNG& g, const param_type& parm);
生成隨機數
返回一個遵循物件關聯的分佈引數(版本1)或由parm指定的引數(版本2)的新的隨機數。

生成器物件(g)透過其operator()成員函式。該 binomial_distribution 物件轉換由此獲取的值,使得該成員函式連續呼叫具有相同引數會產生遵循具有適當引數的二項分佈的值。

引數

g
一個均勻隨機數生成器物件,用作隨機性源。
URNG應為均勻隨機數生成器型別,例如標準生成器類之一。
parm
表示分佈引數的物件,透過呼叫成員函式 param 獲取。
param_type是一個成員型別。

返回值

A new random number. (一個新的隨機數。)
result_type是一個成員型別,定義為第一個類模板引數的別名 (IntType).

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// binomial_distribution example
#include <iostream>
#include <chrono>
#include <random>

int main()
{
  // construct a trivial random generator engine from a time-based seed:
  unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
  std::default_random_engine generator (seed);

  std::binomial_distribution<int> distribution (10,0.5);

  std::cout << "some binomial results (t=10,p=0.5): ";
  for (int i=0; i<10; ++i)
    std::cout << distribution(generator) << " ";

  std::cout << std::endl;

  return 0;
}

可能的輸出
some binomial results (t=10,p=0.5): 5 4 5 4 5 7 5 5 5 6


複雜度

攤銷常數(對g.operator()).

另見