CNL Stat : Random Number Generation : random_sample_indices
random_sample_indices
Generates a simple pseudorandom sample of indices.
Synopsis
#include <imsls.h>
int *imsls_random_sample_indices (int nsamp, int npop, ..., 0)
Required Arguments
int nsamp (Input)
Sample size desired.
int npop (Input)
Number of items in the population.
Return Value
An array of length nsamp containing the indices of the sample. To release this space, use imsls_free.
Synopsis with Optional Arguments
#include <imsls.h>
int *imsls_random_sample_indices (int nsamp, int npop,
IMSLS_RETURN_USER, int ir[],
0)
Optional Arguments
IMSLS_RETURN_USER, int ir[] (Output)
User-supplied array of length nsamp containing the indices of the sample.
Description
Function imsls_random_sample_indices generates the indices of a pseudorandom sample, without replacement, of size nsamp numbers from a population of size npop. If nsamp is greater than npop/2, the integers from 1 to npop are selected sequentially with a probability conditional on the number selected and the number remaining to be considered. If, when the i-th population index is considered, j items have been included in the sample, then the index i is included with probability (nsamp- j)/(npop + 1-i).
If nsamp is not greater than npop/2, a O(nsamp) algorithm due to Ahrens and Dieter (1985) is used. Of the methods discussed by Ahrens and Dieter, the one called SG* is used in imsls_random_sample_indices. It involves a preliminary selection of q indices using a geometric distribution for the distances between each index and the next one. If the preliminary sample size q is less than nsamp, a new preliminary sample is chosen, and this is continued until a preliminary sample greater in size than nsamp is chosen. This preliminary sample is then thinned using the same kind of sampling as described above for the case in which the sample size is greater than half of the population size. Function imsls_random_sample_indices does not store the preliminary sample indices, but rather restores the state of the generator used in selecting the sample initially, and then passes through once again, making the final selection as the preliminary sample indices are being generated.
Example
In this example, imsls_random_sample_indices is used to generate the indices of a pseudorandom sample of size 5 from a population of size 100.
 
#include <imsls.h>
 
int main()
{
int *ir, nsamp = 5, npop = 100;
 
imsls_random_seed_set(123457);
 
ir = imsls_random_sample_indices(nsamp, npop,
0);
 
imsls_i_write_matrix("Random Sample", 1, nsamp, ir,
IMSLS_NO_COL_LABELS,
0);
}
Output
 
Random Sample
2 22 53 61 79