In this article, we will discuss the negativebinomialdistribution in C++ with its syntax, parameters, and examples.
What is the negative_binomial_distribution function in C++?
This function is specified in the randomRandom header. A negative binomial distribution is a random number distribution that produces integers based on a negative binomial discontinuous distribution (also known as the Pascal distribution), where the probability mass function is defined below.
The value shows the number of failures in a sequence of independent yes/no trials (each succeeding with probability p) before particularly k successes occur.
Syntax:
It has the following syntax:
template( class IntType = int )
class negative_bin_distribution
Template Parameter:
- IntType: The generator's results type.
Member functions:
Several member functions of negativebinomialdistribution are as follows:
- Constructor: Build a negative binomial distribution using constructor.
- Operator: It produces a random integer.
- Reset: It resets the distribution.
- Param: Parameters for distribution.
- Min: The lowest possible number.
- Max: The highest possible number.
Constructor
The constructor sets some initialization settings for the Negative Binomial Distribution object. It generally considers the number of successes (r) and the chance of success in just one experiment (p) in the context of the Negative Binomial Distribution. When the distribution object is created, this method configures it and its variables.
Operator
A random number based on the Negative Binomial Distribution is often produced by overloading the operator function. This class provides a random integer that corresponds to the distribution specified by its arguments (r and p) whenever an object of that class is called with parentheses. This function offers a practical method to create random values based on the distribution.
Reset
The distribution's status is reset using the reset function. In simple terms, it restores all internal states or variables that were changed throughout the creation of the random numbers. It ensures that requests to produce random numbers in the future begin from a consistencpp tutorial in the order of distribution.
Parameter
The distribution parameters that were used to generate the Negative Binomial Distribution object are returned by the parameters function. It usually returns a representation of the variables, such as a pair, tuple, or struct, which contains the probability of success in just one experiment (p) and the total amount of successes (r).
Max and Min
The smallest and the greatest values that may be produced from the Negative Binomial Distribution, and these are returned by the min and max functions. When determining the range that the random numbers produced by the distribution can fall within, this information is useful.
Non-member functions: Structure of templates
Operator<< : Inserting into the output stream .
Operator>> : Extracting data from the input stream .
Distribution parameters: public member function
k: The distribution parameter k.
p: The distribution parameter p.
Example 1:
Let us take an example to illustrate the negativebinomialdistribution function in C++.
//Program to generate the random numbers from the Binomial distribution
#include <iostream>
#include <random>
int main() {
// The boundary values for Binomial Distribution
int k = 3; // Failures
double p = 0.4; // The probability of Success
//The random number creator
std::random_device rnd;
std::mt19937 gen(rnd());
//The negative Binomial distribution
std::negative_binomial_distribution<> neg_binom(k, p);
// The random numbers of Distribution
for (int i = 0; i < 10; ++i) {
std::cout << "The Random Value: " << neg_binom(gen) << std::endl;
}
return 0;
}
Output:
The Random Value: 3
The Random Value: 1
The Random Value: 5
The Random Value: 5
The Random Value: 8
The Random Value: 0
The Random Value: 3
The Random Value: 14
The Random Value: 3
The Random Value: 5
Example 2:
Let us take another example to illustrate the negativebinomialdistribution function in C++.
//Program to find the probability mass function of a binomial distribution
#include <iostream>
#include <cmath>
// Function to find the factorial
long long fact(int num) {
if (num== 0 || num == 1) return 1;
long long f = 1;
for (int i = 2; i <= num; ++i) f *= i;
return f;
}
//Function for finding the Ncr value
long long combination(int num, int r) {
return fact(num) / (fact(r) * fact(num - r));
}
// Function to find the negative PMF
double negative_binomial_pmf(int k, int x, double p) {
return combination(k + x - 1, x - 1) * pow(p, x) * pow(1 - p, k);
}
int main() {
int k, x;
double p;
//The input values
std::cout << "Enter the count of failures (k): ";
std::cin >> k;
std::cout << "Enter the count of successes (x): ";
std::cin >> x;
std::cout << "Success Probability (p): ";
std::cin >> p;
// Result
double pmf = negative_binomial_pmf(k, x, p);
std::cout << "Negative Binomial PMF: " << pmf << std::endl;
return 0;
}
Output:
Enter the count of failures (k): 3
Enter the count of successes (x): 5
Success Probability (p): 0.8
Negative Binomial PMF: 0.0917504