公有成員函式 (public member function)
<random>
(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()成員函式。 poisson_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
|
// poisson_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::poisson_distribution<int> distribution (7.1);
std::cout << "some Poisson-distributed results (mean=7.1): ";
for (int i=0; i<10; ++i)
std::cout << distribution(generator) << " ";
std::cout << std::endl;
return 0;
}
|
可能的輸出
some Poisson-distributed results (mean=7.1): 9 8 5 6 6 13 10 14 6 4
|