std::binomial_distribution
来自 cppreference.cn
定义于头文件 <random> |
||
template< class IntType = int > class binomial_distribution; |
(C++11 起) | |
生成随机非负整数值 i,其分布符合离散概率函数:
- P(i|t,p) =⎛
⎜
⎝t
i⎞
⎟
⎠ · pi
· (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) |
构造新的分布 (public member function) |
(C++11) |
重置分布的内部状态 (public member function) |
生成 | |
(C++11) |
生成分布中的下一个随机数 (public member function) |
特性 | |
(C++11) |
返回分布参数 (public member function) |
(C++11) |
获取或设置分布参数对象 (public member function) |
(C++11) |
返回可能生成的最小值 (public member function) |
(C++11) |
返回可能生成的最大值 (public member function) |
[编辑] 非成员函数
(C++11起)(C++11起)(C++20中移除) |
比较两个分布对象 (function) |
(C++11) |
对伪随机数分布执行流输入和输出 (function template) |
[编辑] 示例
二项分布图,每次试验成功的概率正好是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 ******
[编辑] 外部链接
Weisstein, Eric W. "二项分布。" 来自 MathWorld — Wolfram Web 资源。 |