random_binomial
Generates pseudorandom numbers from a binomial distribution.
Synopsis
#include <imsls.h>
int *imsls_f_random_binomial(int n_random, int n, float p, 0)
The type double function is imsls_d_random_binomial.
Required Arguments
int n_random (Input)
Number of random numbers to generate.
int n (Input)
Number of Bernoulli trials.
float p (Input)
Probability of success on each trial. Parameter p must be greater than 0.0 and less than 1.0.
Return Value
An integer array of length n_random containing the random binomial deviates.
Synopsis with Optional Arguments
#include <imsls.h>
int *imsls_f_random_binomial (int n_random, int n, float p,
IMSLS_RETURN_USER, int ir[],
0)
Optional Arguments
IMSLS_RETURN_USER, int ir[] (Output)
User-supplied integer array of length n_random containing the random binomial deviates.
Description
Function imsls_f_random_binomial generates pseudorandom numbers from a binomial distribution with parameters n and p. Parameters n and p must be positive, and p must less than 1. The probability function (with n = n and p = p) is
for x = 0, 1, 2, n.
The algorithm used depends on the values of n and p. If np < 10 or p is less than machine epsilon (see imsls_f_machine, Chapter 15, Utilities), the inverse CDF technique is used; otherwise, the BTPE algorithm of Kachitvichyanukul and Schmeiser (see Kachitvichyanukul 1982) is used. This is an acceptance/rejection method using a composition of four regions. (TPE=Triangle, Parallelogram, Exponential, left and right.)
Example
In this example, imsls_f_random_binomial generates five pseudorandom binomial deviates from a binomial distribution with parameters 20 and 0.5.
 
#include <imsls.h>
 
int main()
{
int n_random = 5;
int n = 20;
float p = 0.5;
int *ir;
 
imsls_random_seed_set(123457);
 
ir = imsls_f_random_binomial(n_random, n, p,
0);
 
imsls_i_write_matrix("Binomial (20, 0.5) random deviates:", 1,
n_random, ir,
IMSLS_NO_COL_LABELS,
0);
}
Output
 
Binomial (20, 0.5) random deviates:
14 9 12 10 12