当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ negative_binomial_distribution用法及代码示例


该函数定义在头文件中随机的随机的。负二项分布是根据负二项式离散分布(也称为帕斯卡分布)生成整数的随机数分布,由以下概率质量函数说明。
P(i|k, p) = \binom{k + i - 1}{i} \cdot p^k \cdot (1 - p)^i
该值表示在恰好发生 k 次成功之前,一系列独立的是/否试验(每次以概率 p 成功)中的失败次数。
用法:

template( class IntType = int )
class negative_binomial_distribution


模板参数:
整数类型:生成器生成的结果类型。
会员类型
会员类型及定义

result_typeIntType
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: **


相关用法


注:本文由纯净天空筛选整理自AmanSrivastava1大神的英文原创作品 negative_binomial_distribution in C++ with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。