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_sample (g05nd)

## Purpose

nag_rand_sample (g05nd) selects a pseudorandom sample without replacement from an integer vector.

## Syntax

[isampl, state, ifail] = g05nd(ipop, m, state, 'n', n)
[isampl, state, ifail] = nag_rand_sample(ipop, m, state, 'n', n)

## Description

nag_rand_sample (g05nd) selects m$m$ elements from a population vector ipop of length n$n$ and places them in a sample vector isampl. Their order in ipop will be preserved in isampl. Each of the
 ( n ) m
$\left(\begin{array}{c}n\\ m\end{array}\right)$ possible combinations of elements of isampl may be regarded as being equally probable.
For moderate or large values of n$n$ it is theoretically impossible that all combinations of size m$m$ may occur, unless m$m$ is near 1 or near n$n$. This is because
 ( n ) m
$\left(\begin{array}{c}n\\ m\end{array}\right)$ exceeds the cycle length of any of the base generators. For practical purposes this is irrelevant, as the time taken to generate all possible combinations is many millenia.
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_sample (g05nd).

## 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:     ipop(n) – int64int32nag_int array
n, the dimension of the array, must satisfy the constraint n1${\mathbf{n}}\ge 1$.
The population to be sampled.
2:     m – int64int32nag_int scalar
The sample size.
Constraint: 1mn$1\le {\mathbf{m}}\le {\mathbf{n}}$.
3:     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:     n – int64int32nag_int scalar
Default: The dimension of the array ipop.
The number of elements in the population vector to be sampled.
Constraint: n1${\mathbf{n}}\ge 1$.

None.

### Output Parameters

1:     isampl(m) – int64int32nag_int array
The selected sample.
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:     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 = 2${\mathbf{ifail}}=2$
On entry, n < 1${\mathbf{n}}<1$.
ifail = 4${\mathbf{ifail}}=4$
 On entry, m < 1${\mathbf{m}}<1$, or m > n${\mathbf{m}}>{\mathbf{n}}$.
ifail = 5${\mathbf{ifail}}=5$
 On entry, state vector was not initialized or has been corrupted.

## Accuracy

Not applicable.

The time taken by nag_rand_sample (g05nd) is of order n$n$.
In order to sample other kinds of vectors, or matrices of higher dimension, the following technique may be used:
 (a) set ipop(i) = i${\mathbf{ipop}}\left(\mathit{i}\right)=\mathit{i}$, for i = 1,2, … ,n$\mathit{i}=1,2,\dots ,n$; (b) use nag_rand_sample (g05nd) to take a sample from ipop and put it into isampl; (c) use the contents of isampl as a set of indices to access the relevant vector or matrix.
In order to divide a population into several groups, nag_rand_permute (g05nc) is more efficient.

## Example

```function nag_rand_sample_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);

ipop = [int64(1);2;3;4;5;6;7;8];
% Initialize the generator to a repeatable sequence
[state, ifail] = nag_rand_init_repeat(genid, subid, seed);
fprintf('\n Samples from the first 8 integers\n\n');
fprintf(' Sample size     Values\n');
isampl = zeros(8, 8, 'int64');
ifail  = zeros(8, 1, 'int64');
for m=1:8
[isampl(m, 1:m), state, ifail(m)] = nag_rand_sample(ipop, int64(m), state);
if ifail == 0
fprintf('     %d           ', m);
fprintf('%d ', isampl(m, 1:m));
fprintf('\n');
end
end
```
```

Samples from the first 8 integers

Sample size     Values
1           2
2           3 6
3           1 5 7
4           2 6 7 8
5           1 2 3 4 8
6           1 3 4 5 6 7
7           1 3 4 5 6 7 8
8           1 2 3 4 5 6 7 8

```
```function g05nd_example
% Initialize the seed
seed = [int64(1762543)];
% genid and subid identify the base generator
genid = int64(1);
subid =  int64(1);

ipop = [int64(1);2;3;4;5;6;7;8];
% Initialize the generator to a repeatable sequence
[state, ifail] = g05kf(genid, subid, seed);
fprintf('\n Samples from the first 8 integers\n\n');
fprintf(' Sample size     Values\n');
isampl = zeros(8, 8, 'int64');
ifail  = zeros(8, 1, 'int64');
for m=1:8
[isampl(m, 1:m), state, ifail(m)] = g05nd(ipop, int64(m), state);
if ifail == 0
fprintf('     %d           ', m);
fprintf('%d ', isampl(m, 1:m));
fprintf('\n');
end
end
```
```

Samples from the first 8 integers

Sample size     Values
1           2
2           3 6
3           1 5 7
4           2 6 7 8
5           1 2 3 4 8
6           1 3 4 5 6 7
7           1 3 4 5 6 7 8
8           1 2 3 4 5 6 7 8

```