公有成員函式 (public member function)
<random>
(1) | explicit binomial_distribution ( result_type t = 1, double p = 0.5 ); |
---|
(2) | explicit binomial_distribution ( const param_type& parm ); |
---|
構造二項分佈 (Construct binomial distribution)
引數
- t
- 範圍的上限 (The upper bound of the range ([0,t])。
這表示每個生成值所模擬的獨立 伯努利分佈 實驗次數。
result_type是一個成員型別,表示每次呼叫 operator() 時生成的隨機數的型別。它被定義為第一個類模板引數的別名(IntType).
- p
- 成功機率。
這表示每次獨立伯努利分佈實驗的成功機率,每個生成的值都模擬了這些實驗。
這應該是一個介於0.0和1.0(兩者都包含)的值。
- parm
- 表示分佈引數的物件,透過呼叫成員函式 param 獲取。
param_type是一個成員型別。
示例
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 5 4 5 4 5 6 4 5 4
|