该函数定义在头文件中随机的随机的。负二项分布是根据负二项式离散分布(也称为帕斯卡分布)生成整数的随机数分布,由以下概率质量函数说明。
该值表示在恰好发生 k 次成功之前,一系列独立的是/否试验(每次以概率 p 成功)中的失败次数。
用法:
template( class IntType = int ) class negative_binomial_distribution
模板参数:
整数类型:生成器生成的结果类型。
会员类型
会员类型及定义
result_type | IntType |
param_type | 成员返回的类型参数 |
会员职能:公共成员函数
- constructor():构造负二项分布
- operator():生成随机数
- reset: 重置分配
- param: 分布参数
- min : 最小值
- max : 最大值
分布参数:公共成员函数
- k :分布参数k
- p :分布参数p
非成员函数:函数模板
- 运算符<<:插入输出流
- 运算符>>:从输入流中提取
- 关系运算符:关系运算符
下面的程序来说明上面的模板
CPP
// C++ program to illustrate
// negative_binomial_distribution
#include <bits/stdc++.h>
using namespace std;
int main()
{
// number of experiments
const int exps = 10000;
// maximum number of stars to distribute
const int numberstars = 100;
// Generator generate numbers based
// upon a generator function
default_random_engine generator;
// Aman watches GOT
// At each episode, there's a 50%
// chance that john snow will die
// after how many time he'll be turned
// away before watching 4 episodes?
negative_binomial_distribution<int> distribution(4, 0.5);
// initializing an array with size 10
int p[10] = {};
for (int i = 0; i < exps; ++i) {
int counting = distribution(generator);
if (counting < 10)
++p[counting];
}
cout << "Negative binomial distribution with "
<< "( k = 4, p = 0.5 ) :" << endl;
// Printing the sequence stored in an array p
for (int i = 0; i < 10; ++i)
cout << i << ": " << string(p[i] * numberstars / exps, '*') << endl;
return 0;
}
输出:
Negative binomial distribution with ( k = 4, p = 0.5 ) : 0: ***** 1: ************ 2: **************** 3: *************** 4: ************* 5: ********** 6: ******** 7: ***** 8: *** 9: **
以下程序的分布参数为 1 和 20%
方案2:
CPP
// C++ program to illustrate
// negative_binomial_distribution
#include <bits/stdc++.h>
using namespace std;
int main()
{
// number of experiments
const int exps = 10000;
// maximum number of stars to distribute
const int numberstars = 100;
// Generator generate numbers based
// upon a generator function
default_random_engine generator;
// Aman watches GOT
// At each episode, there's a
// 20% chance that john snow will die
// after how many time he'll be
// turned away before watching 1 episodes?
negative_binomial_distribution<int> distribution(1, 0.2);
// initializing an array with size 10
int p[10] = {};
for (int i = 0; i < exps; ++i) {
int counting = distribution(generator);
if (counting < 10)
++p[counting];
}
cout << "Negative binomial distribution with "
<< "( k = 1, p = 0.2 ) :" << endl;
// Printing the sequence stored in an array p
for (int i = 0; i < 10; ++i)
cout << i << ": " << string(p[i] * numberstars / exps, '*') << endl;
return 0;
}
输出:
Negative binomial distribution with ( k = 1, p = 0.2 ) : 0: ******************* 1: *************** 2: ************ 3: ********** 4: ******** 5: ****** 6: ***** 7: **** 8: *** 9: **
相关用法
- C++ negate用法及代码示例
- C++ nearbyint()用法及代码示例
- C++ nextafter()用法及代码示例
- C++ nexttoward()用法及代码示例
- C++ nan()用法及代码示例
- C++ none_of()用法及代码示例
- C++ nanf()用法及代码示例
- C++ nanl()用法及代码示例
- C++ norm()用法及代码示例
- C++ cos()用法及代码示例
- C++ sin()用法及代码示例
- C++ asin()用法及代码示例
- C++ atan()用法及代码示例
- C++ atan2()用法及代码示例
- C++ acos()用法及代码示例
- C++ tan()用法及代码示例
- C++ sinh()用法及代码示例
- C++ ceil()用法及代码示例
- C++ tanh()用法及代码示例
- C++ fmod()用法及代码示例
- C++ acosh()用法及代码示例
- C++ asinh()用法及代码示例
- C++ floor()用法及代码示例
- C++ atanh()用法及代码示例
- C++ log()用法及代码示例
注:本文由纯净天空筛选整理自AmanSrivastava1大神的英文原创作品 negative_binomial_distribution in C++ with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。