Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

negative_binomial_distribution in C++ with Examples

  • Last Updated : 16 Dec, 2021

This function is defined in header randomRandom. Negative binomial distribution is Random number distribution that produces integers according to a negative binomial discrete distribution (also known as Pascal distribution), which is described by the following probability mass function. 
P(i|k, p) = \binom{k + i - 1}{i} \cdot p^k \cdot (1 - p)^i
The value represents the number of failures in a series of independent yes/no trials (each succeeds with probability p), before exactly k successes occur. 
Syntax : 

template( class IntType = int )
class negative_binomial_distribution

Template Parameter : 
IntType : The result type generated by the generator.
Member Types 
Member type & Definition 
 

result_typeIntType
param_typeThe type returned by member param

Member functions : public member function 

  • constructor(): Construct negative binomial distribution
  • operator(): Generate random number
  • reset: Reset distribution
  • param: Distribution parameters
  • min : Minimum value
  • max : Maximum value

Distribution parameters : public member function 

  • k :Distribution parameter k 
  • p :Distribution parameter p

Non-member functions : Function Templates 

  • operator<< :Insert into output stream
  • operator>> :Extract from input stream 
  • relational operators : Relational operators

Below program to illustrate the above template 

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;
}

Output : 

Negative binomial distribution with ( k = 4, p = 0.5 ) :
0: *****
1: ************
2: ****************
3: ***************
4: *************
5: **********
6: ********
7: *****
8: ***
9: **

Below program with distribution parameter of 1 and 20% 
Program 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;
}

Output : 

Negative binomial distribution with ( k = 1, p = 0.2 ) :
0: *******************
1: ***************
2: ************
3: **********
4: ********
5: ******
6: *****
7: ****
8: ***
9: **


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!