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$ elements from a population vector ipop of length $n$ and places them in a sample vector isampl. Their order in ipop will be preserved in isampl. Each of the $\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$ it is theoretically impossible that all combinations of size $m$ may occur, unless $m$ is near 1 or near $n$. This is because $\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:     $\mathrm{ipop}\left({\mathbf{n}}\right)$int64int32nag_int array
The population to be sampled.
2:     $\mathrm{m}$int64int32nag_int scalar
The sample size.
Constraint: $1\le {\mathbf{m}}\le {\mathbf{n}}$.
3:     $\mathrm{state}\left(:\right)$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:     $\mathrm{n}$int64int32nag_int scalar
Default: the dimension of the array ipop.
The number of elements in the population vector to be sampled.
Constraint: ${\mathbf{n}}\ge 1$.

### Output Parameters

1:     $\mathrm{isampl}\left({\mathbf{m}}\right)$int64int32nag_int array
The selected sample.
2:     $\mathrm{state}\left(:\right)$int64int32nag_int array
Contains updated information on the state of the generator.
3:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{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:
${\mathbf{ifail}}=2$
Constraint: ${\mathbf{n}}\ge 1$.
${\mathbf{ifail}}=4$
Constraint: $1\le {\mathbf{m}}\le {\mathbf{n}}$.
${\mathbf{ifail}}=5$
On entry, state vector has been corrupted or not initialized.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

Not applicable.

The time taken by nag_rand_sample (g05nd) is of order $n$.
In order to sample other kinds of vectors, or matrices of higher dimension, the following technique may be used:
 (a) set ${\mathbf{ipop}}\left(\mathit{i}\right)=\mathit{i}$, for $\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

In the example program random samples of size $1,2,\dots ,8$ are selected from a vector containing the first eight positive integers in ascending order. The samples are generated and printed for each sample size by a call to nag_rand_sample (g05nd) after initialization by nag_rand_init_repeat (g05kf).
```function g05nd_example

fprintf('g05nd example results\n\n');

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

% Initialize the generator to a repeatable sequence
[state, ifail] = g05kf( ...
genid, subid, seed);

in     = int64(8);
ipop   = int64([1:in]);
isampl = zeros(in, in, 'int64');

fprintf('\n Samples from the first 8 integers\n\n');
fprintf(' Sample size     Values\n');
for m=1:in
[isampl(m, 1:m), state, ifail] = ...
g05nd( ...
ipop, m, state);

fprintf('     %d           ', m);
fprintf('%d ', isampl(m, 1:m));
fprintf('\n');
end

```
```g05nd example results

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