Computer >> Máy Tính >  >> Lập trình >> Lập trình

Phân phối nhị thức phủ định trong cấu trúc dữ liệu

Phân phối nhị thức phủ định là một phân phối số ngẫu nhiên sẽ tạo ra các số nguyên theo phân phối rời rạc nhị thức âm. Đây được gọi là phân phối của Pascal Vì vậy, phân phối nhị thức âm có thể được viết là

$$ P \ lgroup i \ arrowvert k, p \ rgroup =\ lgroup \ frac {k + i-1} {i} \ rgroup p ^ {k} \ lgroup 1-p \ rgroup ^ {i} $$

Ví dụ

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

Đầu ra

0: ************
1: *******************
2: *****************
3: ****************
4: ***********
5: *******
6: *****
7: ***
8: **
9: *