資料結構中的負二項分佈
負二項分佈是一種可以根據負二項離散分佈產生整數的隨機數分佈。這稱為帕斯卡分佈,因此負二項分佈可以寫成
$$P\lgroup i\arrowvert k,p\rgroup=\lgroup \frac{k+i-1}{i}\rgroup p^{k}\lgroup 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: *
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP