Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม

Bernoulli Distribution ในโครงสร้างข้อมูล


นิยาม

Bernoulli Distribution เป็น Discrete Distribution มี 2 ผลลัพธ์: x=0 (Failure), x=1 (Success) โอกาส Success = p, Failure = q = 1-p

สูตร

$$P(x) = \begin{cases} 1-p & \text{for } x = 0 \\ p & \text{for } x = 1 \end{cases}$$

หรือเขียนได้อีกแบบ: $$P(x) = p^{x} (1-p)^{1-x}$$

ตัวอย่าง C++

#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;
    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