“cpp/numeric/random/negative binomial distribution”的版本间的差异
来自cppreference.com
小 (替换文字 - 「{{concept」替换为「{{named req」) |
小 |
||
第1行: | 第1行: | ||
{{cpp/title|negative_binomial_distribution}} | {{cpp/title|negative_binomial_distribution}} | ||
{{cpp/numeric/random/negative_binomial_distribution/navbar}} | {{cpp/numeric/random/negative_binomial_distribution/navbar}} | ||
− | {{ddcl | header=random | since=c++11 | 1= | + | {{ddcl|header=random|since=c++11|1= |
template< class IntType = int > | template< class IntType = int > | ||
class negative_binomial_distribution; | class negative_binomial_distribution; | ||
}} | }} | ||
− | 产生随机非负整数值 {{math|i}} | + | 产生随机非负整数值 {{math|i}},其分布按照离散概率函数: |
− | :{{mathjax-or|1=\(P(i{{!}}k, p) = \binom{k + i - 1}{i} \cdot p^k \cdot (1 - p)^i\)|2=P(i{{!}}k,p) | + | :{{mathjax-or|1=\(P(i{{!}}k, p) = \binom{k + i - 1}{i} \cdot p^k \cdot (1 - p)^i\)|2=P(i{{!}}k,p) ={{mparen|(|)|k + i − 1|i}} · p{{su|p=k}} · (1 − p){{su|p=i}}}} |
− | 该值表示一系列独立的是/否试验在准确出现 {{math|k}} 次成功前的失败次数(每次成功概率为 {{math|p}} | + | 该值表示一系列独立的是/否试验在准确出现 {{math|k}} 次成功前的失败次数(每次成功概率为 {{math|p}})。 |
− | {{tt|std::negative_binomial_distribution}} 满足{{named req|RandomNumberDistribution | + | {{tt|std::negative_binomial_distribution}} 满足{{named req|RandomNumberDistribution}}。 |
===模板形参=== | ===模板形参=== | ||
第21行: | 第21行: | ||
===成员类型=== | ===成员类型=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc hitem |成员类型|定义}} | + | {{dsc hitem|成员类型|定义}} |
− | {{dsc | {{tt|result_type}} | + | {{dsc|{{tt|result_type}} {{c|IntType}}}} |
{{cpp/numeric/random/param_type}} | {{cpp/numeric/random/param_type}} | ||
{{dsc end}} | {{dsc end}} | ||
第28行: | 第28行: | ||
===成员函数=== | ===成员函数=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/numeric/random/distribution/dsc constructor | negative_binomial_distribution}} | + | {{dsc inc|cpp/numeric/random/distribution/dsc constructor|negative_binomial_distribution}} |
− | {{dsc inc | cpp/numeric/random/distribution/dsc reset | negative_binomial_distribution}} | + | {{dsc inc|cpp/numeric/random/distribution/dsc reset|negative_binomial_distribution}} |
− | {{dsc h2 |生成}} | + | {{dsc h2|生成}} |
− | {{dsc inc | cpp/numeric/random/distribution/dsc operator() | negative_binomial_distribution}} | + | {{dsc inc|cpp/numeric/random/distribution/dsc operator()|negative_binomial_distribution}} |
− | {{dsc h2 |特征}} | + | {{dsc h2|特征}} |
− | {{dsc inc | cpp/numeric/random/distribution/dsc params | negative_binomial_distribution}} | + | {{dsc inc|cpp/numeric/random/distribution/dsc params|negative_binomial_distribution}} |
− | {{dsc inc | cpp/numeric/random/distribution/dsc param | negative_binomial_distribution}} | + | {{dsc inc|cpp/numeric/random/distribution/dsc param|negative_binomial_distribution}} |
− | {{dsc inc | cpp/numeric/random/distribution/dsc min | negative_binomial_distribution}} | + | {{dsc inc|cpp/numeric/random/distribution/dsc min|negative_binomial_distribution}} |
− | {{dsc inc | cpp/numeric/random/distribution/dsc max | negative_binomial_distribution}} | + | {{dsc inc|cpp/numeric/random/distribution/dsc max|negative_binomial_distribution}} |
{{dsc end}} | {{dsc end}} | ||
===非成员函数=== | ===非成员函数=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/numeric/random/distribution/dsc operator_cmp | negative_binomial_distribution }} | + | {{dsc inc|cpp/numeric/random/distribution/dsc operator_cmp|negative_binomial_distribution}} |
− | {{dsc inc | cpp/numeric/random/distribution/dsc operator_ltltgtgt | negative_binomial_distribution}} | + | {{dsc inc|cpp/numeric/random/distribution/dsc operator_ltltgtgt|negative_binomial_distribution}} |
{{dsc end}} | {{dsc end}} | ||
===示例=== | ===示例=== | ||
{{example | {{example | ||
− | + | |code= | |
− | + | ||
#include <iomanip> | #include <iomanip> | ||
− | #include < | + | #include <> |
#include <map> | #include <map> | ||
#include <random> | #include <random> | ||
+ | |||
int main() | int main() | ||
第66行: | 第66行: | ||
std::map<int, int> hist; | std::map<int, int> hist; | ||
− | for(int n=0; n | + | for (int n = 0; n 10000; ++n) |
++hist[d(gen)]; | ++hist[d(gen)]; | ||
− | + | ||
− | for(auto | + | for (auto : hist) |
− | std::cout | + | std::cout << << ' ' << std::string(/ 100, '*') << '\n'; |
− | + | ||
} | } | ||
− | + | ||
+ | |output= | ||
0 *********************** | 0 *********************** | ||
1 ***************************** | 1 ***************************** | ||
第81行: | 第81行: | ||
5 *** | 5 *** | ||
6 * | 6 * | ||
− | 7 | + | 7 |
− | 8 | + | 8 |
− | 9 | + | 9 |
− | + | ||
− | + | ||
}} | }} | ||
===外部链接=== | ===外部链接=== | ||
− | [ | + | [://mathworld.wolfram.com/NegativeBinomialDistribution.html Weisstein, Eric W. “负二项分布。”]来自 MathWorld--A Wolfram Web Resource。 |
{{langlinks|de|en|es|fr|it|ja|pt|ru}} | {{langlinks|de|en|es|fr|it|ja|pt|ru}} |
2024年2月19日 (一) 10:52的最后版本
在标头 <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、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)(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。 |