std:: binomial_distribution
From cppreference.net
|
定义于头文件
<random>
|
||
|
template
<
class
IntType
=
int
>
class binomial_distribution ; |
(C++11 起) | |
生成随机非负整数值 i ,按照离散概率函数分布:
-
P(i|t,p) =
⎛
⎜
⎝ t
i ⎞
⎟
⎠ · p i
· (1 − p) t−i
该值表示在一系列 t 次是/非实验中成功次数的统计量,其中每次实验的成功概率为 p 。
std::binomial_distribution
满足
RandomNumberDistribution
要求。
目录 |
模板参数
| IntType | - | 生成器产生的结果类型。若该类型不是 short 、 int 、 long 、 long long 、 unsigned short 、 unsigned int 、 unsigned long 或 unsigned long long 中的任意一种,则其效果是未定义的。 |
成员类型
| 成员类型 | 定义 |
result_type
(C++11)
|
IntType |
param_type
(C++11)
|
参数集的类型,参见 RandomNumberDistribution 。 |
成员函数
|
(C++11)
|
构造新分布
(公开成员函数) |
|
(C++11)
|
重置分布的内部状态
(公开成员函数) |
生成 |
|
|
(C++11)
|
生成分布中的下一个随机数
(公开成员函数) |
特性 |
|
|
(C++11)
|
返回分布参数
(公开成员函数) |
|
(C++11)
|
获取或设置分布参数对象
(公开成员函数) |
|
(C++11)
|
返回可能生成的最小值
(公开成员函数) |
|
(C++11)
|
返回可能生成的最大值
(公开成员函数) |
非成员函数
|
(C++11)
(C++11)
(removed in C++20)
|
比较两个分布对象
(函数) |
|
(C++11)
|
对伪随机数分布执行流输入和输出操作
(函数模板) |
示例
展示成功概率恰好为0.5的二项分布图,说明其与帕斯卡三角形的关联(本例中4次试验成功次数为0、1、2、3或4次的概率比为1:4:6:4:1)。
运行此代码
#include <iomanip> #include <iostream> #include <map> #include <random> #include <string> int main() { std::random_device rd; std::mt19937 gen(rd()); // perform 4 trials, each succeeds 1 in 2 times std::binomial_distribution<> d(4, 0.5); std::map<int, int> hist; for (int n = 0; n != 10000; ++n) ++hist[d(gen)]; for (auto const& [x, y] : hist) std::cout << x << ' ' << std::string(y / 100, '*') << '\n'; }
可能的输出:
0 ****** 1 ************************ 2 ************************************* 3 ************************* 4 ******
外部链接
| 魏斯坦, 埃里克·W. "二项分布." 摘自 MathWorld —— 一个 Wolfram 网络资源。 |