# IMSL_RANDOM_SAMPLE

The IMSL_RANDOM_SAMPLE function generates a simple pseudorandom sample from a finite population.

Note: This routine requires an IDL Analyst license. For more information, contact your Exelis VIS sales or technical support representative.

Routine IMSL_RANDOM_SAMPLE generates a pseudorandom sample from a given population, without replacement, using an algorithm due to McLeod and Bellhouse (1983).

The first nsamp items in the population are included in the sample. Then, for each successive item from the population, a random item in the sample is replaced by that item from the population with probability equal to the sample size divided by the number of population items that have been encountered at that time.

## Examples

For additional information on using IMSL_RANDOM_SAMPLE, see Additional Examples.

### Example 1

In this example, IMSL_RANDOM_SAMPLE is used to generate a sample of size 5 from a population stored in the matrix population.

IMSL_RANDOMOPT, Set = 123457

pop = IMSL_STATDATA(2)

samp = IMSL_RANDOM_SAMPLE(5, pop)

PM, samp

1764.00 36.4000

1828.00 62.5000

1923.00 5.80000

1773.00 34.8000

1769.00 106.100

## Syntax

Result = IMSL_RANDOM_SAMPLE(nsamp, population [, /ADDITIONAL_CALL] [, /DOUBLE] [, /FIRST_CALL] [, INDEX=array] [, NPOP=value] [, SAMPLE=array] )

## Return Value

nsamp by nvar array containing the sample, where nvar is the number of columns in the argument population.

## Arguments

### nsamp

The sample size desired.

### population

A one or two dimensional array containing the population to be sampled. If either of the keywords FIRST_CALL or ADDITIONAL_CALL are specified, then population contains a different part of the population on each invocation, otherwise population contains the entire population.

## Keywords

### ADDITIONAL_CALL

If present and nonzero, then this is an additional invocation of IMSL_RANDOM_SAMPLE, and updating for the subpopulation in population is performed. Keywords Index, NPOP, and Sample are required if ADDITIONAL_CALL is set.

It is not necessary to know the number of items in the population in advance. NPOP is used to cumulate the population size and should not be changed between calls to IMSL_RANDOM_SAMPLE. See Example 2.

### DOUBLE

If present and nonzero, double precision is used.

### FIRST_CALL

If present and nonzero, then this is the first invocation with this data; additional calls to IMSL_RANDOM_SAMPLE may be made to add to the population. Additional calls should be made using the keyword ADDITIONAL_CALL. Keywords Index and NPOP are required if FIRST_CALL is set. See Example 2.

### INDEX

A one-dimensional array of length nsamp containing the indices of the sample in the population. Output if keyword FIRST_CALL is used. Input/Output if keyword ADDITIONAL_CALL is used.

### NPOP

The number of items in the population. Output if keyword FIRST_CALL is used. Input/Output if keyword ADDITIONAL_CALL is used.

### SAMPLE

An array of size nsamp by nvar containing the sample. Initially, the result of calling IMSL_RANDOM_SAMPLE with keyword FIRST_CALL is used for Sample.

## Additional Examples

### Example 2

Routine IMSL_RANDOM_SAMPLE is now used to generate a sample of size 5 from the same population as in the example above except the data are input to IMSL_RANDOM_SAMPLE one observation at a time. This is the way IMSL_RANDOM_SAMPLE may be used to sample from a file on disk or tape.

Notice that the number of records need not be known in advance.

IMSL_RANDOMOPT, SET = 123457

pop = IMSL_STATDATA(2)

samp = IMSL_RANDOM_SAMPLE(5, pop(0, *), /FIRST_CALL, INDEX = ii, $

NPOP=np)

FOR i=1,175 DO samp = IMSL_RANDOM_SAMPLE(5, pop(i, *), $

/ADDITIONAL_CALL, INDEX = ii, NPOP = np, SAMPLE = samp)

PM, samp

1764.00 36.4000

1828.00 62.5000

1923.00 5.80000

1773.00 34.8000

1769.00 106.100

## Version History

6.4 | Introduced

## See Also