Trong hướng dẫn này, ta thảo luận về negative_binomial_distribution trong C++.
Hàm này tuân theo phân phối nhị âm rời rạc và tạo ra số nguyên theo phân phối ngẫu nhiên này.
Ví dụ
#include <bits/stdc++.h> using namespace std; int main() { const int exps = 10000; const int numberstars = 100; default_random_engine generator; negative_binomial_distribution<int> distribution(4, 0.5); int p[10] = {}; for (int i = 0; i < exps; ++i) { int counting = distribution(generator); if (counting < 10) ++p[counting]; } cout << "Negative binomial distribution with " << "( k = 4, p = 0.5 ) :" << endl; for (int i = 0; i < 10; ++i) cout << i << ": " << string(p[i] * numberstars / exps, '*') << endl; return 0; }Đầu ra
Negative binomial distribution with ( k = 4, p = 0.5 ) : 0: ***** 1: ************ 2: **************** 3: *************** 4: ************* 5: ********** 6: ******** 7: ***** 8: *** 9: **