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
该值表示一系列独立的是/否试验在准确出现 k 次成功前的失败次数(每次成功概率为 p)。
std::negative_binomial_distribution
满足随机数分布(RandomNumberDistribution) 。
目录 |
[编辑]模板形参
IntType | - | 生成器所生成的结果类型。如果它不是 short、int、long、longlong、unsignedshort、unsignedint、unsignedlong 或 unsignedlonglong 之一,那么效果未定义。 |
[编辑]成员类型
成员类型 | 定义 |
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)(C++20 移除) | 比较两个分布对象 (函数) |
(C++11) | 执行伪随机数分布的流输入和输出 (函数模板) |
[编辑]示例
运行此代码
#include <iomanip>#include <iostream>#include <map>#include <random>#include <string> int main(){std::random_device rd;std::mt19937 gen(rd());// Pat 挨家挨户卖饼干// 在每家,有 75% 的几率卖出一盒// 试问她在卖出 5 盒前要卖多少次? 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[x, y]: hist)std::cout<<std::hex<< x <<' '<<std::string(y /100, '*')<<'\n';}
可能的输出:
0 *********************** 1 ***************************** 2 ********************** 3 ************* 4 ****** 5 *** 6 * 7 8 9 a b
[编辑]外部链接
Weisstein, Eric W. “负二项分布。”来自 MathWorld--A Wolfram Web Resource。 |