음의 이항 분포는 음의 이항 이산 분포에 따라 정수를 생성하는 난수 분포입니다. 이것은 파스칼 분포로 알려져 있으므로 음의 이항 분포는 다음과 같이 쓸 수 있습니다.
$$P\l그룹 i\arrowvert k,p\rgroup=\l그룹 \frac{k+i-1}{i}\r그룹 p^{k}\l그룹 1-p\rgroup^{i}$$피>
예시
#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;
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;
} 출력
0: ************ 1: ******************* 2: ***************** 3: **************** 4: *********** 5: ******* 6: ***** 7: *** 8: ** 9: *