g05 Chapter Contents
g05 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_ref_vec_binomial (g05edc)

## 1  Purpose

nag_ref_vec_binomial (g05edc) sets up the reference vector r for a binomial distribution of the number of successes in $n$ trials, each with probability of success $p$.

## 2  Specification

 #include #include
 void nag_ref_vec_binomial (Integer n, double p, double **r, NagError *fail)

## 3  Description

nag_ref_vec_binomial (g05edc) sets up a reference vector for use in nag_return_discrete (g05eyc). Together these functions produce random numbers from the binomial distribution defined by:
 $P I=i = n! i! n-i ! p i 1-p n-i if ​ i = 0 , … , n, P I=i = 0 otherwise.$
The reference array is found by a recurrence relation if $np\left(1-p\right)<50$; otherwise Stirling's approximation is used.

## 4  References

Kendall M G and Stuart A (1969) The Advanced Theory of Statistics (Volume 1) (3rd Edition) Griffin
Knuth D E (1981) The Art of Computer Programming (Volume 2) (2nd Edition) Addison–Wesley

## 5  Arguments

1:     nIntegerInput
On entry: the number of trials, $n$, of the distribution.
Constraint: ${\mathbf{n}}\ge 0$.
2:     pdoubleInput
On entry: the probability of success, $p$, of the distribution.
Constraint: $0.0\le {\mathbf{p}}\le 1.0$.
3:     rdouble **Output
On exit: reference vector for which memory will be allocated internally. If no memory is allocated to r (e.g., when an input error is detected) then r will be NULL on return, otherwise you should use the NAG macro NAG_FREE to free the storage allocated by r when it is no longer of use.
4:     failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
NE_INT_ARG_LT
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 0$.
NE_REAL_ARG_GT
On entry, p must not be greater than 1.0: ${\mathbf{p}}=〈\mathit{\text{value}}〉$.
NE_REAL_ARG_LT
On entry, p must not be less than 0.0: ${\mathbf{p}}=〈\mathit{\text{value}}〉$.

Not applicable.

None.

## 9  Example

The example program sets up a reference vector for a binomial distribution with $n=100$ and $p=0.5$; it then prints the first five pseudorandom numbers generated by nag_return_discrete (g05eyc), after initialization by nag_random_init_repeatable (g05cbc).

### 9.1  Program Text

Program Text (g05edce.c)

None.

### 9.3  Program Results

Program Results (g05edce.r)