帶示例在 C++ 中負二項式分佈


在本教程中,我們將討論一個程式來了解 C++ 中的負二項式分佈。

此函式遵循負二項式離散分佈,並且根據此隨機分佈生成整數。

示例

 現場演示

#include <bits/stdc++.h>
using namespace std;
int main() {
   //setting number of experiments
   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;
   //printing the sequence from the array
   for (int i = 0; i < 10; ++i)
      cout << i << ": " << string(p[i] * numberstars / exps, '*') << endl;
   return 0;
}

結果

Negative binomial distribution with ( k = 4, p = 0.5 ) :
0: *****
1: ************
2: ****************
3: ***************
4: *************
5: **********
6: ********
7: *****
8: ***
9: **

更新於: 06-4-2020

119 個瀏覽量

開啟您的 職業

透過完成此課程獲得認證

開始
廣告
© . All rights reserved.