带示例在 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.