数据结构中的几何分布
几何分布是离散概率分布,其中 n = 0, 1, 2, …,具有概率密度函数。
$$P\left( n\right)=p\left(1-p\right)^{n}$$
分布函数为 −
$$D\left( n\right)=\displaystyle\sum\limits_{i=0}^n P\left( i \right)=1-q^{n+1}$$
示例
#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; geometric_distribution<int> distribution(0.3); int p[10]={}; for (int i=0; i<nrolls; ++i) { int number = distribution(generator); if (number<10) p[number]++; } cout << "Geometric_distribution (0.3):" << 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: *
广告