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_general (g05td)

## Purpose

nag_rand_int_general (g05td) generates a vector of pseudorandom integers from a discrete distribution with a given PDF (probability density function) or CDF (cumulative distribution function) p$p$.

## Syntax

[r, state, x, ifail] = g05td(mode, n, p, ip1, itype, r, state, 'np', np)
[r, state, x, ifail] = nag_rand_int_general(mode, n, p, ip1, itype, r, state, 'np', np)

## Description

nag_rand_int_general (g05td) generates a sequence of n$n$ integers xi${x}_{i}$, from a discrete distribution defined by information supplied in p. This may either be the PDF or CDF of the distribution. A reference vector is first set up to contain the CDF of the distribution in its higher elements, followed by an index.
Setting up the reference vector and subsequent generation of variates can each be performed by separate calls to nag_rand_int_general (g05td) or may be combined in a single call.
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_general (g05td).

## 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

## 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_general (g05td).
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(np) – double array
np, the dimension of the array, must satisfy the constraint np > 0${\mathbf{np}}>0$.
The PDF or CDF of the distribution.
Constraints:
• 0.0p(i)1.0$0.0\le {\mathbf{p}}\left(\mathit{i}\right)\le 1.0$, for i = 1,2,,np$\mathit{i}=1,2,\dots ,{\mathbf{np}}$;
• if itype = 1${\mathbf{itype}}=1$, i = 1np p(i) = 1.0$\sum _{\mathit{i}=1}^{{\mathbf{np}}}{\mathbf{p}}\left(\mathit{i}\right)=1.0$;
• if itype = 2${\mathbf{itype}}=2$, p(i) < p(j), ​i < j​ and ​p(np) = 1.0${\mathbf{p}}\left(\mathit{i}\right)<{\mathbf{p}}\left(j\right)\text{, ​}\mathit{i}.
4:     ip1 – int64int32nag_int scalar
The value of the variate, a whole number, to which the probability in p(1)${\mathbf{p}}\left(1\right)$ corresponds.
5:     itype – int64int32nag_int scalar
Indicates the type of information contained in p.
itype = 1${\mathbf{itype}}=1$
p contains a probability distribution function (PDF).
itype = 2${\mathbf{itype}}=2$
p contains a cumulative distribution function (CDF).
Constraint: itype = 1${\mathbf{itype}}=1$ or 2$2$.
6:     r(lr) – double array
lr, the dimension of the array, must satisfy the constraint
• if mode = 0${\mathbf{mode}}=0$ or 2$2$, lrnp + 8$\mathit{lr}\ge {\mathbf{np}}+8$;
• if mode = 1${\mathbf{mode}}=1$, lr should remain unchanged from the previous call to nag_rand_int_general (g05td).
If mode = 1${\mathbf{mode}}=1$, the reference vector from the previous call to nag_rand_int_general (g05td).
7:     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.

### Optional Input Parameters

1:     np – int64int32nag_int scalar
Default: The dimension of the array p.
The number of values supplied in p defining the PDF or CDF of the discrete distribution.
Constraint: np > 0${\mathbf{np}}>0$.

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
Contains n$n$ pseudorandom numbers from the specified discrete 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$ or 2$2$.
ifail = 2${\mathbf{ifail}}=2$
On entry, n < 0${\mathbf{n}}<0$.
ifail = 3${\mathbf{ifail}}=3$
With itype = 1${\mathbf{itype}}=1$, p(j) < 0${\mathbf{p}}\left(j\right)<0$ for at least one value of j$j$.
With itype = 1${\mathbf{itype}}=1$, the sum of p(j)${\mathbf{p}}\left(\mathit{j}\right)$, for j = 1,2,,np$\mathit{j}=1,2,\dots ,{\mathbf{np}}$, does not equal 1$1$.
With itype = 2${\mathbf{itype}}=2$, the values of p(j)${\mathbf{p}}\left(j\right)$ are not all in non-descending order.
ifail = 4${\mathbf{ifail}}=4$
On entry, np < 1${\mathbf{np}}<1$.
ifail = 6${\mathbf{ifail}}=6$
On entry, itype1${\mathbf{itype}}\ne 1$ or 2$2$.
ifail = 7${\mathbf{ifail}}=7$
The value of np, itype or ip1 is not the same as when r was set up in a previous call to nag_rand_int_general (g05td) with mode = 0${\mathbf{mode}}=0$ or 2$2$.
On entry, the r vector was not initialized correctly, or has been corrupted.
ifail = 8${\mathbf{ifail}}=8$
On entry, lr is too small when mode = 0${\mathbf{mode}}=0$ or 2$2$.
ifail = 9${\mathbf{ifail}}=9$
 On entry, state vector was not initialized or has been corrupted.

Not applicable.

None.

## Example

```function nag_rand_int_general_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);
mode = int64(2);
n = int64(20);
p = [0.01;
0.02;
0.04;
0.08;
0.2;
0.3;
0.2;
0.08;
0.04;
0.02;
0.01];
ip1 = int64(-5);
itype = int64(1);
r = zeros(60, 1);
% Initialize the generator to a repeatable sequence
[state, ifail] = nag_rand_init_repeat(genid, subid, seed);
[r, state, x, ifail] = nag_rand_int_general(mode, n, p, ip1, itype, r, state)
```
```

r =

1.0e+03 *

9.9995
0.0605
0.0115
-0.0055
0.0015
0.0115
-0.0065
0.0410
0.0000
0.0000
0.0001
0.0002
0.0004
0.0006
0.0009
0.0009
0.0010
0.0010
0.0010
0.0015
0.0025
0.0035
0.0045
0.0045
0.0045
0.0045
0.0055
0.0055
0.0055
0.0055
0.0055
0.0055
0.0055
0.0055
0.0065
0.0065
0.0065
0.0065
0.0065
0.0065
0.0065
0.0065
0.0065
0.0065
0.0065
0.0065
0.0075
0.0075
0.0075
0.0075
0.0075
0.0075
0.0075
0.0075
0.0085
0.0085
0.0085
0.0085
0.0095
0.0105

state =

17
1234
1
0
6694
27818
10435
15383
17917
13895
19930
8
0
1234
1
1
1234

x =

0
-2
1
1
-2
0
0
1
0
1
-3
-1
0
-3
0
-1
-1
5
2
0

ifail =

0

```
```function g05td_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);
mode = int64(2);
n = int64(20);
p = [0.01;
0.02;
0.04;
0.08;
0.2;
0.3;
0.2;
0.08;
0.04;
0.02;
0.01];
ip1 = int64(-5);
itype = int64(1);
r = zeros(60, 1);
% Initialize the generator to a repeatable sequence
[state, ifail] = g05kf(genid, subid, seed);
[r, state, x, ifail] = g05td(mode, n, p, ip1, itype, r, state)
```
```

r =

1.0e+03 *

9.9995
0.0605
0.0115
-0.0055
0.0015
0.0115
-0.0065
0.0410
0.0000
0.0000
0.0001
0.0002
0.0004
0.0006
0.0009
0.0009
0.0010
0.0010
0.0010
0.0015
0.0025
0.0035
0.0045
0.0045
0.0045
0.0045
0.0055
0.0055
0.0055
0.0055
0.0055
0.0055
0.0055
0.0055
0.0065
0.0065
0.0065
0.0065
0.0065
0.0065
0.0065
0.0065
0.0065
0.0065
0.0065
0.0065
0.0075
0.0075
0.0075
0.0075
0.0075
0.0075
0.0075
0.0075
0.0085
0.0085
0.0085
0.0085
0.0095
0.0105

state =

17
1234
1
0
6694
27818
10435
15383
17917
13895
19930
8
0
1234
1
1
1234

x =

0
-2
1
1
-2
0
0
1
0
1
-3
-1
0
-3
0
-1
-1
5
2
0

ifail =

0

```