randomBinomial

Generates pseudorandom numbers from a binomial distribution.

Synopsis

randomBinomial(nRandom, n, p)

Required Arguments

int nRandom (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 nRandom containing the random binomial deviates.

Description

Function randomBinomial 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

\[f(x) = \binom{n}{x} p^x(1-p)^{n-x}\]

for \(x=0,1,2,\ldots,n\).

The algorithm used depends on the values of n and p. If \(np<10\) or p is less than machine epsilon (see 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, randomBinomial generates five pseudorandom binomial deviates from a binomial distribution with parameters 20 and 0.5.

from numpy import *
from pyimsl.stat.randomBinomial import randomBinomial
from pyimsl.stat.randomSeedSet import randomSeedSet
from pyimsl.stat.writeMatrix import writeMatrix

n_random = 5
n = 20
p = 0.5
randomSeedSet(123457)
ir = randomBinomial(n_random, n, p)
writeMatrix("Binomial (20, 0.5) random deviates:", ir,
            noColLabels=True)

Output

 
              Binomial (20, 0.5) random deviates:
         14            9           12           10           12