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

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


นิยาม

Negative Binomial Distribution เป็นการแจกแจงสุ่มที่สร้างจำนวนเต็มตาม Negative Binomial Discrete Distribution รู้จักในชื่อ Pascal's Distribution

สูตร

$$P(i|k,p) = \binom{k+i-1}{i} p^{k} (1-p)^{i}$$

ตัวอย่าง C++

#include <iostream>
#include <random>
using namespace std;

int main() {
    const int nrolls = 10000;
    const int nstars = 100;
    default_random_engine generator;
    negative_binomial_distribution<int> distribution(3, 0.5);
    int p[10] = {};
    for (int i = 0; i < nrolls; ++i) {
        int number = distribution(generator);
        if (number < 10) p[number]++;
    }
    cout << "negative_binomial_distribution (3,0.5):" << 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: *