Phân phối Bernoulli là một phân phối rời rạc có hai kết quả có thể xảy ra được gắn nhãn x =0 và x =1. X =1 là thành công và x =0 là thất bại. Thành công xảy ra với xác suất p và thất bại xảy ra với xác suất q là q =1 - p. Vì vậy,
$$ P \ lgroup x \ rgroup =\ begin {case} 1-p \:for &x =0 \\ p \:for &x =0 \ end {case} $$
Điều này cũng có thể được viết là -
$$ P \ lgroup x \ rgroup =p ^ {n} \ lgroup1-p \ rgroup ^ {1-n} $$
Ví dụ
#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;
} Đầu ra
bernoulli_distribution (0.7) x 10000: true:7024 false: 2976