nag_ran_sample_vec (g05ejc) (PDF version)
g05 Chapter Contents
g05 Chapter Introduction
NAG C Library Manual

NAG Library Function Document

nag_ran_sample_vec (g05ejc)

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

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

2  Specification

#include <nag.h>
#include <nagg05.h>
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 n m  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: n1 .
3:     iz[m]IntegerOutput
On exit: the selected sample.
4:     mIntegerInput
On entry: the sample size.
Constraint: 1 m n .
5:     failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

6  Error Indicators and Warnings

On entry, m=value  while n=value . These arguments must satisfy mn .
On entry, m=value.
Constraint: m1.
On entry, n=value.
Constraint: n1.

7  Accuracy

Not applicable.

8  Further Comments

If n  is greater than 60 it is theoretically impossible to generate all n m  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 ia[i-1] = i , for i=1,2,,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,,8 are selected and printed.

9.1  Program Text

Program Text (g05ejce.c)

9.2  Program Data


9.3  Program Results

Program Results (g05ejce.r)

nag_ran_sample_vec (g05ejc) (PDF version)
g05 Chapter Contents
g05 Chapter Introduction
NAG C Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2012