Negative Binomial distribution in Data Structures


The Negative Binomial Distribution is a random number distribution that will produce integers according to a negative binomial discrete distribution. This is known as Pascal’s distribution So the negative binomial distribution can be written as

$$P\lgroup i\arrowvert k,p\rgroup=\lgroup \frac{k+i-1}{i}\rgroup p^{k}\lgroup 1-p\rgroup^{i}$$

Example

 Live Demo

#include <iostream>
#include <random>
using namespace std;
int main(){
   const int nrolls = 10000; // number of rolls
   const int nstars = 100; // maximum number of stars to distribute
   default_random_engine generator;
   negative_binomial_distribution<int> distribution(3,0.5);
   int p[10]={};
   for (int i=0; i<nrolls; ++i) {
      int number = distribution(generator);
   if (number<10)
      p[number]++;
   }
   cout << "negative_binomial_distribution (3,0.5):" << endl;
   for (int i=0; i<10; ++i)
      cout << i << ": " << string(p[i]*nstars/nrolls,'*') << endl;
}

Output

0: ************
1: *******************
2: *****************
3: ****************
4: ***********
5: *******
6: *****
7: ***
8: **
9: *

Updated on: 27-Aug-2019

79 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements