當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。