std:: negative_binomial_distribution
From cppreference.net
|
定义于头文件
<random>
|
||
|
template
<
class
IntType
=
int
>
class negative_binomial_distribution ; |
(C++11 起) | |
生成随机非负整数值 i ,按照离散概率函数分布:
-
P(i|k,p) =
⎛
⎜
⎝ k + i − 1
i ⎞
⎟
⎠ · p k
· (1 − p) i
该值表示在一系列独立的是/非试验(每次试验成功概率为 p )中,在恰好出现 k 次成功之前所经历的失败次数。
std::negative_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)
|
对伪随机数分布执行流输入和输出操作
(函数模板) |
示例
运行此代码
#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
外部链接
| 魏斯坦, 埃里克·W。《负二项分布》。 摘自 MathWorld —— 一个 Wolfram 网络资源。 |