g05 Chapter Contents
g05 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_ran_sample_vec (g05ejc)

## 1  Purpose

nag_ran_sample_vec (g05ejc) selects a pseudorandom sample without replacement from an integer vector.

## 2  Specification

 #include #include
 void nag_ran_sample_vec (const Integer ia[], Integer n, Integer iz[], Integer m, NagError *fail)

## 3  Description

nag_ran_sample_vec (g05ejc) performs a single pseudorandom selection of $m$ elements from vector ia of length $n$ and then places them in vector iz. Their order in ia will be preserved in iz. Each of the $\begin{array}{c}n\\ m\end{array}$ possible combinations of elements of ia may be regarded as being equiprobable.

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

## 5  Arguments

1:     ia[n]const IntegerInput
On entry: the population to be sampled.
2:     nIntegerInput
On entry: the number of elements in the vector to be sampled.
Constraint: ${\mathbf{n}}\ge 1$.
3:     iz[m]IntegerOutput
On exit: the selected sample.
4:     mIntegerInput
On entry: the sample size.
Constraint: $1\le {\mathbf{m}}\le {\mathbf{n}}$.
5:     failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_2_INT_ARG_GT
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$ while ${\mathbf{n}}=〈\mathit{\text{value}}〉$. These arguments must satisfy ${\mathbf{m}}\le {\mathbf{n}}$.
NE_INT_ARG_LT
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m}}\ge 1$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 1$.

## 7  Accuracy

Not applicable.

If $n$ is greater than 60 it is theoretically impossible to generate all $\begin{array}{c}n\\ m\end{array}$ combinations unless $m$ is near 1 or near $n$. This is because the number of possible combinations exceeds the cycle length of the internal random number generator.
The time taken by nag_ran_sample_vec (g05ejc) is of order $n$.
In order to sample other kinds of objects (i.e., vectors, or matrices of higher dimensions), the following technique may be used:
 (a) Set ${\mathbf{ia}}\left[\mathit{i}-1\right]=\mathit{i}$, for $\mathit{i}=1,2,\dots ,n$ (where $n$ is the number of objects) (b) Use nag_ran_sample_vec (g05ejc) to take a sample from ia and put it into iz (c) Use the contents of iz as a set of indices to access the relevant object.
In order to divide a population into several groups, nag_ran_permut_vec (g05ehc) is more efficient.

## 9  Example

From a vector containing $0$ and the first $7$ positive integers in ascending order, random samples of size $1,2,\dots ,8$ are selected and printed.

### 9.1  Program Text

Program Text (g05ejce.c)

None.

### 9.3  Program Results

Program Results (g05ejce.r)