std::negative_binomial_distribution
来自cppreference.com
![]() |
该页由英文版维基使用谷歌翻译机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击此处。 |
在标头 <random> 定义
|
||
template< class IntType = int > class negative_binomial_distribution; |
(C++11 起) | |
产生随机的非负整数值i根据离散概率函数,分布:
- P(i|k,p) =⎛
⎜
⎝k + i − 1
i⎞
⎟
⎠ · pk
· (1 − p)i
该值表示在一系列独立“是/否”试验失败的次数(每个成功的概率p),之前究竟发生k成功.
目录 |
会员类型
Definition | |
result_type
|
IntType |
param_type
|
成员函数
(C++11) |
构造新分布 (公开成员函数) |
(C++11) |
重置分布的内部状态 (公开成员函数) |
| |
(C++11) |
生成分布中的下个随机数 (公开成员函数) |
| |
(C++11) |
返回分布参数 (公开成员函数) |
(C++11) |
返回分布参数 (公开成员函数) |
(C++11) |
获取或设置随机参数对象 (公开成员函数) |
(C++11) |
返回潜在生成的最小值 (公开成员函数) |
(C++11) |
返回潜在生成的最大值 (公开成员函数) |
非成员函数
(C++11)(C++11)(C++20 移除) |
比较两个分布对象 (函数) |
(C++11) |
执行伪随机数分布的流输入和输出 (函数模板) |
为例
运行此代码
#include <iostream> #include <iomanip> #include <string> #include <map> #include <random> int main() { std::random_device rd; std::mt19937 gen(rd()); // Pat goes door-to-door selling cookies // At each house, there's a 75% chance that she sells one box // how many times will she be turned away before selling 5 boxes? std::negative_binomial_distribution<> d(5, 0.75); 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
外部链接
Weisstein, Eric W. "Negative Binomial Distribution.",从MathWorld - Wolfram网络资源.
原文:
Weisstein, Eric W. "Negative Binomial Distribution." From MathWorld--A Wolfram Web Resource.