RNNBN
Generates pseudorandom numbers from a negative binomial distribution.
Required Arguments
RK — Negative binomial parameter. (Input)
RK must be positive.
P — Probability of success on each trial. (Input)
P must be greater than the machine epsilon, AMACH(4) (Reference Material) and less than 1.0.
IR — Vector of length NR containing the random negative binomial deviates. (Output)
Optional Arguments
NR — Number of random numbers to generate. (Input)
Default: NR = size (IR,1).
FORTRAN 90 Interface
Generic: CALL RNNBN (RK, P, IR [])
Specific: The specific interface name is S_RNNBN.
FORTRAN 77 Interface
Single: CALL RNNBN (NR, RK, P, IR)
Description
Routine RNNBN generates pseudorandom numbers from a negative binomial distribution with parameters RK and P. RK and P must be positive and P must be less than 1. The probability function (with r = RK and p = P) is
for x = 0, 1, 2, .
If r is an integer, the distribution is often called the Pascal distribution and can be thought of as modeling the length of a sequence of Bernoulli trials until r successes are obtained, where p is the probability of getting a success on any trial. In this form, the random variable takes values rr + 1, r + 2,  and can be obtained from the negative binomial random variable defined above by adding r to the negative binomial variable. This latter form is also equivalent to the sum of r geometric random variables defined as taking values 1, 2, 3,  .
If rp/(1  p) is less than 100 and (1  p)r is greater than the machine epsilon, RNNBN uses the inverse CDF technique; otherwise, for each negative binomial deviate, RNNBN generates a gamma (rp/(1  p)) deviate Y and then generates a Poisson deviate with parameter Y.
Comments
1. The routine RNSET can be used to initialize the seed of the random number generator. The routine RNOPT can be used to select the form of the generator.
2. If RK is an integer, the deviates in IR can be thought of as the number of failures in a sequence of Bernoulli trials before RK successes occur.
Example
In this example, RNNBN is used to generate five pseudorandom deviates from a negative binomial (Pascal) distribution with parameter r equal to 4 and p equal to 0.3.
 
USE RNNBN_INT
USE UMACH_INT
USE RNSET_INT
 
IMPLICIT NONE
INTEGER NR
PARAMETER (NR=5)
!
INTEGER IR(NR), ISEED, NOUT
REAL P, RK
!
CALL UMACH (2, NOUT)
P = 0.3
RK = 4.0
ISEED = 123457
CALL RNSET (ISEED)
CALL RNNBN (RK, P, IR)
WRITE (NOUT,99999) IR
99999 FORMAT (' Negative binomial (4.0, 0.3) random deviates: ', 5I4)
END
Output
 
Negative binomial (4.0, 0.3) random deviates: 5 1 3 2 3