📜  C++中的negative_binomial_distribution与示例

📅  最后修改于: 2021-05-30 17:00:10             🧑  作者: Mango

此函数在标头随机Random中定义。负二项式分布是根据负二项式离散分布(也称为Pascal分布)生成整数的随机数分布,其由以下概率质量函数描述。
 P(i|k, p) = \binom{k + i - 1}{i} \cdot p^k \cdot (1 - p)^i
该值表示在完全进行k次成功之前,在一系列独立的是/否试验(每次成功均以概率p)中失败的次数。
句法 :

template( class IntType = int )
class negative_binomial_distribution

模板参数:
IntType:生成器生成的结果类型。

会员类型
会员类型和定义

result_type IntType
param_type The type returned by member param

成员职能:公共成员函数

  • Constructor():构造负二项分布
  • operstor():生成随机数
  • 重置:重置分布
  • 参数:分布参数
  • min:最小值
  • max:最大值

    分布参数:公共成员函数

  • k:分布参数k
  • p:分布参数p

    非成员函数:函数模板

  • 运算符<<:插入输出流
  • 运算符>>:从输入流中提取
  • 关系运算符:关系运算符

    下面的程序来说明上面的模板

    // C++ program to illustrate
    // negative_binomial_distribution
    #include 
    using namespace std;
      
    int main()
    {
        // number of experiments
        const int exps = 10000;
      
        // maximum number of stars to distribute
        const int numberstars = 100;
      
        // Generator generate numbers based
        // upon a generator function
        default_random_engine generator;
      
        // Aman watches GOT
        // At each episode, there's a 50%
        // chance that john snow will die
        // after how many time he'll be turned
        // away before watching 4 episodes?
        negative_binomial_distribution distribution(4, 0.5);
      
        // initializing an array with size 10
        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 stored in an array p
        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: **
    

    下面的程序,分配参数为1和20%
    程式2:

    // C++ program to illustrate
    // negative_binomial_distribution
    #include 
    using namespace std;
      
    int main()
    {
        // number of experiments
        const int exps = 10000;
      
        // maximum number of stars to distribute
        const int numberstars = 100;
      
        // Generator generate numbers based
        // upon a generator function
        default_random_engine generator;
      
        // Aman watches GOT
        // At each episode, there's a
        // 20% chance that john snow will die
        // after how many time he'll be
        // turned away before watching 1 episodes?
        negative_binomial_distribution distribution(1, 0.2);
      
        // initializing an array with size 10
        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 = 1, p = 0.2 ) :" << endl;
      
        // Printing the sequence stored in an array p
        for (int i = 0; i < 10; ++i)
            cout << i << ": " << string(p[i] * numberstars / exps, '*') << endl;
      
        return 0;
    }
    

    输出 :

    Negative binomial distribution with ( k = 1, p = 0.2 ) :
    0: *******************
    1: ***************
    2: ************
    3: **********
    4: ********
    5: ******
    6: *****
    7: ****
    8: ***
    9: **
    
    
    要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”