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_hypergeom (g05te)

## Purpose

nag_rand_int_hypergeom (g05te) generates a vector of pseudorandom integers from the discrete hypergeometric distribution of the number of specified items in a sample of size l$l$, taken from a population of size k$k$ with m$m$ specified items in it.

## Syntax

[r, state, x, ifail] = g05te(mode, n, ns, np, m, r, state)
[r, state, x, ifail] = nag_rand_int_hypergeom(mode, n, ns, np, m, r, state)

## Description

nag_rand_int_hypergeom (g05te) generates n$n$ integers xi${x}_{i}$ from a discrete hypergeometric distribution, where the probability of xi = I${x}_{i}=I$ is
 P(i = I) = (l ! m ! (k − l) ! (k − m) ! )/(I ! (l − I) ! (m − I) ! (k − m − l + I) ! k ! ) if  I = max (0,m + l − k) , … , min (l,m) , P(i = I) = 0 otherwise.
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_hypergeom (g05te) with the same parameter values can then use this reference vector to generate further variates. The reference array is generated by a recurrence relation if lm(kl)(km) < 50k3$lm\left(k-l\right)\left(k-m\right)<50{k}^{3}$, otherwise Stirling's approximation 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_hypergeom (g05te).

## 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_hypergeom (g05te).
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:     ns – int64int32nag_int scalar
l$l$, the sample size of the hypergeometric distribution.
Constraint: 0nsnp$0\le {\mathbf{ns}}\le {\mathbf{np}}$.
4:     np – int64int32nag_int scalar
k$k$, the population size of the hypergeometric distribution.
Constraint: np0${\mathbf{np}}\ge 0$.
5:     m – int64int32nag_int scalar
m$m$, the number of specified items of the hypergeometric distribution.
Constraint: 0mnp$0\le {\mathbf{m}}\le {\mathbf{np}}$.
6:     r(lr) – double array
lr, the dimension of the array, must satisfy the constraint
• if mode = 0${\mathbf{mode}}=0$ or 2$2$, lr must not be too small, but the limit is too complicated to specify;
• if mode = 1${\mathbf{mode}}=1$, lr must remain unchanged from the previous call to nag_rand_int_hypergeom (g05te).
If mode = 1${\mathbf{mode}}=1$, the reference vector from the previous call to nag_rand_int_hypergeom (g05te).
If mode = 3${\mathbf{mode}}=3$, r is not referenced by nag_rand_int_hypergeom (g05te).
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.

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 pseudorandom numbers from the specified hypergeometric 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, ns > np${\mathbf{ns}}>{\mathbf{np}}$, or ns < 0${\mathbf{ns}}<0$.
ifail = 4${\mathbf{ifail}}=4$
On entry, np < 0${\mathbf{np}}<0$.
ifail = 5${\mathbf{ifail}}=5$
 On entry, m > np${\mathbf{m}}>{\mathbf{np}}$, or m < 0${\mathbf{m}}<0$.
ifail = 6${\mathbf{ifail}}=6$
On entry, at least one of ns, np or m is not the same as when r was set up in a previous call to nag_rand_int_hypergeom (g05te) with mode = 0${\mathbf{mode}}=0$ or 2$2$.
On entry, the r vector was not initialized correctly or has been corrupted.
ifail = 7${\mathbf{ifail}}=7$
On entry, lr is too small when mode = 0${\mathbf{mode}}=0$ or 2$2$.
ifail = 8${\mathbf{ifail}}=8$
 On entry, state vector was not initialized or has been corrupted.

Not applicable.

None.

## Example

```function nag_rand_int_hypergeom_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);
ns = int64(500);
np = int64(1000);
m = int64(900);
r = zeros(200, 1);
% Initialize the generator to a repeatable sequence
[state, ifail] = nag_rand_init_repeat(genid, subid, seed);
[r, state, x, ifail] = nag_rand_int_hypergeom(mode, n, ns, np, m, r, state)
```
```

r =

1.0e+03 *

3.6925
0.2005
0.5005
1.0005
0.9005
0.0775
0.4155
0.1150
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0001
0.0001
0.0001
0.0002
0.0002
0.0003
0.0004
0.0005
0.0005
0.0006
0.0007
0.0008
0.0008
0.0009
0.0009
0.0009
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0015
0.0245
0.0255
0.0265
0.0265
0.0275
0.0275
0.0285
0.0285
0.0285
0.0295
0.0295
0.0295
0.0295
0.0295
0.0305
0.0305
0.0305
0.0305
0.0305
0.0315
0.0315
0.0315
0.0315
0.0315
0.0315
0.0315
0.0325
0.0325
0.0325
0.0325
0.0325
0.0325
0.0325
0.0325
0.0335
0.0335
0.0335
0.0335
0.0335
0.0335
0.0335
0.0335
0.0335
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0355
0.0355
0.0355
0.0355
0.0355
0.0355
0.0355
0.0355
0.0355
0.0355
0.0365
0.0365
0.0365
0.0365
0.0365
0.0365
0.0365
0.0365
0.0365
0.0375
0.0375
0.0375
0.0375
0.0375
0.0375
0.0375
0.0375
0.0375
0.0385
0.0385
0.0385
0.0385
0.0385
0.0385
0.0385
0.0385
0.0395
0.0395
0.0395
0.0395
0.0395
0.0395
0.0395
0.0405
0.0405
0.0405
0.0405
0.0405
0.0415
0.0415
0.0415
0.0415
0.0415
0.0425
0.0425
0.0425
0.0435
0.0435
0.0445
0.0445
0.0455
0.0465

state =

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

x =

452
444
453
454
444
450
449
454
450
452
442
447
451
442
451
447
447
462
456
450

ifail =

0

```
```function g05te_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);
ns = int64(500);
np = int64(1000);
m = int64(900);
r = zeros(200, 1);
% Initialize the generator to a repeatable sequence
[state, ifail] = g05kf(genid, subid, seed);
[r, state, x, ifail] = g05te(mode, n, ns, np, m, r, state)
```
```

r =

1.0e+03 *

3.6925
0.2005
0.5005
1.0005
0.9005
0.0775
0.4155
0.1150
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0001
0.0001
0.0001
0.0002
0.0002
0.0003
0.0004
0.0005
0.0005
0.0006
0.0007
0.0008
0.0008
0.0009
0.0009
0.0009
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0010
0.0015
0.0245
0.0255
0.0265
0.0265
0.0275
0.0275
0.0285
0.0285
0.0285
0.0295
0.0295
0.0295
0.0295
0.0295
0.0305
0.0305
0.0305
0.0305
0.0305
0.0315
0.0315
0.0315
0.0315
0.0315
0.0315
0.0315
0.0325
0.0325
0.0325
0.0325
0.0325
0.0325
0.0325
0.0325
0.0335
0.0335
0.0335
0.0335
0.0335
0.0335
0.0335
0.0335
0.0335
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0345
0.0355
0.0355
0.0355
0.0355
0.0355
0.0355
0.0355
0.0355
0.0355
0.0355
0.0365
0.0365
0.0365
0.0365
0.0365
0.0365
0.0365
0.0365
0.0365
0.0375
0.0375
0.0375
0.0375
0.0375
0.0375
0.0375
0.0375
0.0375
0.0385
0.0385
0.0385
0.0385
0.0385
0.0385
0.0385
0.0385
0.0395
0.0395
0.0395
0.0395
0.0395
0.0395
0.0395
0.0405
0.0405
0.0405
0.0405
0.0405
0.0415
0.0415
0.0415
0.0415
0.0415
0.0425
0.0425
0.0425
0.0435
0.0435
0.0445
0.0445
0.0455
0.0465

state =

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

x =

452
444
453
454
444
450
449
454
450
452
442
447
451
442
451
447
447
462
456
450

ifail =

0

```