Bernoulli Distribution in Data Structures

Data Structure AlgorithmsData Structure and AlgorithmsAlgorithms

The Bernoulli Distribution is a discrete distribution 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

$$P\lgroup x\rgroup=\begin{cases}1-p\:for & x = 0\\p\:for & x = 0\end{cases}$$

This can also be written as −

$$P\lgroup x\rgroup=p^{n}\lgroup1-p\rgroup^{1-n}$$

Example

 Live Demo

#include <iostream>
#include <random>
using namespace std;
int main(){
   const int nrolls=10000;
   default_random_engine generator;
   bernoulli_distribution distribution(0.7);
   int count=0; // count number of trues
   for (int i=0; i<nrolls; ++i)
      if (distribution(generator))
      count++;
   cout << "bernoulli_distribution (0.7) x 10000:" << endl;
   cout << "true: " << count << endl;
   cout << "false: " << nrolls-count << endl;
}

Output

bernoulli_distribution (0.7) x 10000:
true:7024
false: 2976
raja
Published on 27-Aug-2019 16:03:29
Advertisements