# Binomial Distribution in Data Structures

The Binomial Distribution is a discrete probability distribution Pp(n | N) of obtaining n successes out of N Bernoulli trails (having two possible outcomes labeled by x = 0 and x = 1. The x = 1 is success, and x = 0 is failure. Success occurs with probability p, and failure occurs with probability q as q = 1 – p.) So the binomial distribution can be written as

$$P_{p}\lgroup n\:\arrowvert\ N\rgroup=\left(\begin{array}{c}N\ n\end{array}\right) p^{n}\lgroup1-p\rgroup^{N-n}$$

## 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;
binomial_distribution<int> distribution(9,0.5);
int p[10]={};
for (int i=0; i<nrolls; ++i) {
int number = distribution(generator);
p[number]++;
}
cout << "binomial_distribution (9,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: