Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_rand_int_geom (g05tc)

## Purpose

nag_rand_int_geom (g05tc) generates a vector of pseudorandom integers from the discrete geometric distribution with probability p$p$ of success at a trial.

## Syntax

[r, state, x, ifail] = g05tc(mode, n, p, r, state)
[r, state, x, ifail] = nag_rand_int_geom(mode, n, p, r, state)

## Description

nag_rand_int_geom (g05tc) generates n$n$ integers xi${x}_{i}$ from a discrete geometric distribution, where the probability of xi = I${x}_{i}=I$ (a first success after I + 1$I+1$ trials) is
 P (xi = I) = p × (1 − p)I ,   I = 0,1, … . $P (xi=I) = p × (1-p) I , I=0,1,… .$
The variates can be generated with or without using a search table and index. If a search table is used then it is stored with the index in a reference vector and subsequent calls to nag_rand_int_geom (g05tc) with the same parameter value can then use this reference vector to generate further variates. If the search table is not used (as recommended for small values of p$p$) then a direct transformation of uniform variates is used.
One of the initialization functions nag_rand_init_repeat (g05kf) (for a repeatable sequence if computed sequentially) or nag_rand_init_nonrepeat (g05kg) (for a non-repeatable sequence) must be called prior to the first call to nag_rand_int_geom (g05tc).

## References

Knuth D E (1981) The Art of Computer Programming (Volume 2) (2nd Edition) Addison–Wesley

## Parameters

### Compulsory Input Parameters

1:     mode – int64int32nag_int scalar
A code for selecting the operation to be performed by the function.
mode = 0${\mathbf{mode}}=0$
Set up reference vector only.
mode = 1${\mathbf{mode}}=1$
Generate variates using reference vector set up in a prior call to nag_rand_int_geom (g05tc).
mode = 2${\mathbf{mode}}=2$
Set up reference vector and generate variates.
mode = 3${\mathbf{mode}}=3$
Generate variates without using the reference vector.
Constraint: mode = 0${\mathbf{mode}}=0$, 1$1$, 2$2$ or 3$3$.
2:     n – int64int32nag_int scalar
n$n$, the number of pseudorandom numbers to be generated.
Constraint: n0${\mathbf{n}}\ge 0$.
3:     p – double scalar
The parameter p$p$ of the geometric distribution representing the probability of success at a single trial.
Constraint: machine precisionp1.0 (see nag_machine_precision (x02aj)).
4:     r(lr) – double array
lr, the dimension of the array, must satisfy the constraint
• if mode = 0${\mathbf{mode}}=0$ or 2$2$, lr30 / p + 8$\mathit{lr}\ge 30/{\mathbf{p}}+8$;
• if mode = 1${\mathbf{mode}}=1$, lr should remain unchanged from the previous call to nag_rand_int_geom (g05tc).
If mode = 1${\mathbf{mode}}=1$, the reference vector from the previous call to nag_rand_int_geom (g05tc).
If mode = 3${\mathbf{mode}}=3$, r is not referenced by nag_rand_int_geom (g05tc).
5:     state( : $:$) – int64int32nag_int array
Note: the actual argument supplied must be the array state supplied to the initialization routines nag_rand_init_repeat (g05kf) or nag_rand_init_nonrepeat (g05kg).
Contains information on the selected base generator and its current state.

None.

lr

### Output Parameters

1:     r(lr) – double array
The reference vector.
2:     state( : $:$) – int64int32nag_int array
Note: the actual argument supplied must be the array state supplied to the initialization routines nag_rand_init_repeat (g05kf) or nag_rand_init_nonrepeat (g05kg).
Contains updated information on the state of the generator.
3:     x(n) – int64int32nag_int array
The n$n$ pseudorandom numbers from the specified geometric distribution.
4:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 1${\mathbf{ifail}}=1$
On entry, mode0${\mathbf{mode}}\ne 0$, 1$1$, 2$2$ or 3$3$.
ifail = 2${\mathbf{ifail}}=2$
On entry, n < 0${\mathbf{n}}<0$.
ifail = 3${\mathbf{ifail}}=3$
 On entry, p < 0.0${\mathbf{p}}<0.0$ or p > 1.0${\mathbf{p}}>1.0$, or mode = 0${\mathbf{mode}}=0$ or 2$2$ and p is so small that lr would have to be larger than the largest representable integer. Use mode = 3${\mathbf{mode}}=3$ in this case.
ifail = 4${\mathbf{ifail}}=4$
On entry, p is not the same as when r was set up in a previous call to nag_rand_int_geom (g05tc) with mode = 0${\mathbf{mode}}=0$ or 2$2$.
On entry, the r vector was not initialized correctly or has been corrupted.
ifail = 5${\mathbf{ifail}}=5$
On entry, lr is too small when mode = 0${\mathbf{mode}}=0$ or 2$2$.
ifail = 6${\mathbf{ifail}}=6$
 On entry, state vector was not initialized or has been corrupted.

## Accuracy

Not applicable.

The time taken to set up the reference vector, if used, increases with the length of array r. However, if the reference vector is used, the time taken to generate numbers decreases as the space allotted to the index part of r increases. Nevertheless, there is a point, depending on the distribution, where this improvement becomes very small and the suggested value for the length of array r is designed to approximate this point.
If p is very small then the storage requirements for the reference vector and the time taken to set up the reference vector becomes prohibitive. In this case it is recommended that the reference vector is not used. This is achieved by selecting mode = 3${\mathbf{mode}}=3$.

## Example

```function nag_rand_int_geom_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);
mode = int64(3);
n = int64(10);
p = 0.001;
r = zeros(40000, 1);
% Initialize the generator to a repeatable sequence
[state, ifail] = nag_rand_init_repeat(genid, subid, seed);
[r, state, x, ifail] = nag_rand_int_geom(mode, n, p, r, state);
x
ifail
```
```

x =

451
2238
292
225
2256
708
955
239
696
397

ifail =

0

```
```function g05tc_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);
mode = int64(3);
n = int64(10);
p = 0.001;
r = zeros(40000, 1);
% Initialize the generator to a repeatable sequence
[state, ifail] = g05kf(genid, subid, seed);
[r, state, x, ifail] = g05tc(mode, n, p, r, state);
x
ifail
```
```

x =

451
2238
292
225
2256
708
955
239
696
397

ifail =

0

```