# negative_binomial_distribution in C++ with Examples

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. 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_type IntType param_type The type returned by member param

Member functions : public member function

• constructor(): Construct negative binomial distribution
• operstor(): 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

 // C++ program to illustrate  // negative_binomial_distribution  #include  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 = {};         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:

 // C++ program to illustrate  // negative_binomial_distribution  #include  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 = {};         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 Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.