Geometric Distribution in Data Structures

Data Structure AlgorithmsAnalysis of AlgorithmsAlgorithms

The Geometric Distribution is a discrete probability distribution for n = 0, 1, 2, …. having the probability density function.

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

The distribution function is −

$$D\lgroup n\rgroup=\displaystyle\sum\limits_{i=0}^n P\lgroup i \rgroup=1-q^{n+1}$$

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;
   geometric_distribution<int> distribution(0.3);
   int p[10]={};
   for (int i=0; i<nrolls; ++i) {
      int number = distribution(generator);
      if (number<10)
      p[number]++;
   }
   cout << "Geometric_distribution (0.3):" << 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: *
raja
Published on 27-Aug-2019 16:20:58
Advertisements