NAG Library Function Document
nag_rand_sample (g05ndc) selects a pseudorandom sample without replacement from an integer vector.
||nag_rand_sample (const Integer ipop,
nag_rand_sample (g05ndc) selects
elements from a population vector ipop
and places them in a sample vector isampl
. Their order in ipop
will be preserved in isampl
. Each of the
possible combinations of elements of isampl
may be regarded as being equally probable.
For moderate or large values of it is theoretically impossible that all combinations of size may occur, unless is near 1 or near . This is because 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_repeatable (g05kfc)
(for a repeatable sequence if computed sequentially) or nag_rand_init_nonrepeatable (g05kgc)
(for a non-repeatable sequence) must be called prior to the first call to nag_rand_sample (g05ndc).
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
ipop[n] – const IntegerInput
On entry: the population to be sampled.
n – IntegerInput
On entry: the number of elements in the population vector to be sampled.
isampl[m] – IntegerOutput
On exit: the selected sample.
m – IntegerInput
On entry: the sample size.
state – IntegerCommunication Array
, of this array is dictated by the requirements of associated functions that must have been previously called. This array MUST be the same array passed as argument state
in the previous call to nag_rand_init_repeatable (g05kfc)
or nag_rand_init_nonrepeatable (g05kgc)
On entry: contains information on the selected base generator and its current state.
On exit: contains updated information on the state of the generator.
fail – NagError *Input/Output
The NAG error argument (see Section 3.6
in the Essential Introduction).
6 Error Indicators and Warnings
On entry, argument had an illegal value.
On entry, .
On entry, and .
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG
On entry, state
vector has been corrupted or not initialized.
8 Parallelism and Performance
The time taken by nag_rand_sample (g05ndc) is of order .
In order to sample other kinds of vectors, or matrices of higher dimension, the following technique may be used:
||set , for ;
||use nag_rand_sample (g05ndc) to take a sample from ipop and put it into isampl;
||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 (g05ncc)
is more efficient.
In the example program random samples of size
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 (g05ndc) after initialization by nag_rand_init_repeatable (g05kfc)
10.1 Program Text
Program Text (g05ndce.c)
10.2 Program Data
10.3 Program Results
Program Results (g05ndce.r)