Negative Binomial Distribution In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Negative Binomial Distribution In C++

Negative Binomial Distribution In C++

BLUF: Mastering Negative Binomial Distribution In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Negative Binomial Distribution In C++

C++ is renowned for its efficiency. Learn how Negative Binomial Distribution In C++ enables low-level control and high-performance computing in the tutorial below.

In this guide, we will explore the negativebinomialdistribution in C++ along with its syntax, parameters, and illustrations.

What is the negative_binomial_distribution function in C++?

This function is defined in the randomRandom header file. A negative binomial distribution generates integer values following a negative binomial discrete distribution (also called the Pascal distribution). The probability mass function for this distribution is described as follows:

The value represents the count of failures in a series of independent trials with a yes/no outcome, where each trial has a probability of success denoted as p, before reaching a specific total of k successful outcomes.

Syntax:

It has the following syntax:

Example

template( class IntType = int )
class negative_bin_distribution

Template Parameter:

  • IntType: The data type of the outcomes produced by the generator.
  • 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 initialization method establishes initial parameters for the Negative Binomial Distribution instance, focusing on the count of successes (r) and the probability of success in a single trial (p) within the Negative Binomial Distribution framework. Upon instantiation of the distribution object, this function initializes the object and its associated variables.

Operator

A random number following the Negative Binomial Distribution is commonly generated by overloading the operator method. This particular class delivers a random integer that aligns with the specified distribution parameters (r and p) each time an instance of this class is invoked using parentheses. Such a mechanism presents a convenient approach for generating random values according to the specified distribution.

Reset

The reset function is employed to reset the status of the distribution. Put simply, it reverts all altered internal states or variables that occurred during the generation of random numbers. This guarantees that future requests for generating random numbers start from a consistent state in the distribution sequence.

Parameter

The parameters function retrieves the distribution parameters employed in creating the Negative Binomial Distribution object. Typically, it provides a data structure like a pair, tuple, or struct, comprising the success probability of a single trial (p) and the total number of successes (r).

Max and Min

The lowest and the highest values that can be generated by the Negative Binomial Distribution are provided by the min and max functions. Knowing this range is valuable when establishing the possible range of random numbers generated by this distribution.

Non-member functions: Structure of templates

Operator<< : Inserting into the output stream .

Operator> : Retrieving information from the input stream.

Distribution parameters: public member function

k: The distribution parameter k.

p: The distribution parameter p.

Example 1:

Let's consider a scenario to demonstrate the negativebinomialdistribution function in C++.

Example

//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:

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's consider another scenario to demonstrate the negativebinomialdistribution function within C++.

Example

//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:

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

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience