Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Bernoulli Distribution in Data Structures
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
#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
Advertisements