negative_binomial_distribution in C++ with Examples
Last Updated :
16 Dec, 2021
This function is defined in header randomRandom. Negative binomial distribution is Random number distribution that produces integers according to a negative binomial discrete distribution (also known as Pascal distribution), which is described by the following probability mass function.
The value represents the number of failures in a series of independent yes/no trials (each succeeds with probability p), before exactly k successes occur.
Syntax :
template( class IntType = int )
class negative_binomial_distribution
Template Parameter :
IntType : The result type generated by the generator.
Member Types
Member type & Definition
result_type | IntType |
param_type | The type returned by member param |
Member functions : public member function
- constructor(): Construct negative binomial distribution
- operator(): Generate random number
- reset: Reset distribution
- param: Distribution parameters
- min : Minimum value
- max : Maximum value
Distribution parameters : public member function
- k :Distribution parameter k
- p :Distribution parameter p
Non-member functions : Function Templates
- operator<< :Insert into output stream
- operator>> :Extract from input stream
- relational operators : Relational operators
Below program to illustrate the above template
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
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;
for ( int i = 0; i < 10; ++i)
cout << i << ": " << string(p[i] * numberstars / exps, '*' ) << endl;
return 0;
}
|
Output :
Negative binomial distribution with ( k = 4, p = 0.5 ) :
0: *****
1: ************
2: ****************
3: ***************
4: *************
5: **********
6: ********
7: *****
8: ***
9: **
Below program with distribution parameter of 1 and 20%
Program 2:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
const int exps = 10000;
const int numberstars = 100;
default_random_engine generator;
negative_binomial_distribution< int > distribution(1, 0.2);
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;
for ( int i = 0; i < 10; ++i)
cout << i << ": " << string(p[i] * numberstars / exps, '*' ) << endl;
return 0;
}
|
Output :
Negative binomial distribution with ( k = 1, p = 0.2 ) :
0: *******************
1: ***************
2: ************
3: **********
4: ********
5: ******
6: *****
7: ****
8: ***
9: **
Share your thoughts in the comments
Please Login to comment...