C++ 参考手册

位置:首页 > C++ 参考手册 >数值库 >伪随机数生成 > std::poisson_distribution

定义于头文件 <random>
template< class IntType = int >
class poisson_distribution;
(C++11 起)

产生随机非负整数值 i ,分布服从离散概率函数:

P(i|μ) =
e
·μi
i!

若同一条件下(相同的时间/空间区间上)随机事件的期望平均发生次数为 μ ,则获得的值是其准确发生 i 次的概率。

std::poisson_distribution 满足随机数分布 (RandomNumberDistribution)

模板形参

IntType - 生成器所生成的结果类型。若它不是 shortintlonglong longunsigned shortunsigned intunsigned longunsigned long long 之一则效果未定义。


成员类型

 
成员类型 定义
result_type IntType
param_type 参数集的类型,见随机数分布 (RandomNumberDistribution)

成员函数

构造新分布
(公开成员函数)
重置分布的内部状态
(公开成员函数)
生成
生成分布中的下个随机数
(公开成员函数)
特征
返回 mean 分布参数(事件出现次数的平均数)
(公开成员函数)
获取或设置随机参数对象
(公开成员函数)
返回最小的潜在生成值
(公开成员函数)
返回最大的潜在生成值
(公开成员函数)

非成员函数

比较两个分布对象
(函数)
执行伪随机数分布的流输入和输出
(函数模板)

示例

#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <random>
int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
 
    // 若平均一分钟出现 4 次事件
    // 则在一分钟内出现 n 次的频率如何?
    std::poisson_distribution<> d(4);
 
    std::map<int, int> hist;
    for(int n=0; n<10000; ++n) {
        ++hist[d(gen)];
    }
    for(auto p : hist) {
        std::cout << p.first <<
                ' ' << std::string(p.second/100, '*') << '\n';
    }
}

输出:

0 *
1 *******
2 **************
3 *******************
4 *******************
5 ***************
6 **********
7 *****
8 **
9 *
10 
11 
12 
13

外部链接

Weisstein, Eric W. “泊松分布。”来自 MathWorld--A Wolfram Web Resource 。