RNSRS
Generates a simple pseudorandom sample from a finite population.
Required Arguments
POPNROW by NVAR matrix containing the population to be sampled. (Input) If IDO = 0, POP contains the entire population; otherwise, POP contains a different part of the population on each invocation of RNSRS.
NPOP — The number of items in the population. (Output, if IDO = 0 or 1; Input/Output, if IDO = 2.)
If IDO = 0, NPOP = NROW on output. If the population is input a few items at a time, 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 RNSRS. If, on output, NPOP is greater than or equal to NSAMP, the sampling can be considered complete for a population of size NPOP.
SAMPNSAMP by NVAR matrix containing the sample. (Output, if IDO = 0 or 1; Input/Output, if IDO = 2.)
INDEX — Vector of length NSAMP containing the indices of the sample in the population. (Output, if IDO = 0 or 1; Input/Output, if IDO = 2.) The INDEX(I)-th item in the population is the I‑th item in the sample. INDEX is not necessarily in increasing order.
Optional Arguments
IDO — Processing option. (Input)
Default: IDO = 0.
IDO
Action
0
This is the only invocation of RNSRS for this data set, and the entire population is input at once.
1
This is the first invocation, and additional calls to RNSRS will be made. Initialization and updating for the subpopulation in POP are performed.
2
This is an additional invocation of RNSRS, and updating for the subpopulation in POP is performed.
NROW — Number of rows of data currently input in POP. (Input)
NROW must be nonnegative.
Default: NROW = size (POP,1).
NVAR — Number of variables in the population and in the sample. (Input)
Default: NVAR = size (POP,2).
LDPOP — Leading dimension of POP exactly as specified in the dimension statement in the calling program. (Input)
Default: LDPOP = size (POP,1).
NSAMP — The sample size desired. (Input)
Default: NSAMP = size (SAMP,1).
LDSAMP — Leading dimension of SAMP exactly as specified in the dimension statement in the calling program. (Input)
Default: LDSAMP = size (SAMP,1).
FORTRAN 90 Interface
Generic: CALL RNSRS (POP, NPOP, SAMP, INDEX [])
Specific: The specific interface names are S_RNSRS and D_RNSRS.
FORTRAN 77 Interface
Single: CALL RNSRS (IDO, NROW, NVAR, POP, LDPOP, NSAMP, NPOP, SAMP, LDSAMP, INDEX)
Double: The double precision name is DRNSRS.
Description
Routine RNSRS 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.
Comments
1. The routine RNSET can be used to initialize the seed of the random number generator. The routine RNOPT can be used to select the form of the generator.
2. The routine RNSRI can be used to select a sample of indices in increasing order.
Examples
Example 1
In this example, RNSRS is used to generate a sample of size 5 from a population stored in the matrix POP. All of the data are available at once, so default IDO = 0 is used.
 
USE RNSRS_INT
USE UMACH_INT
USE GDATA_INT
USE RNSET_INT
 
IMPLICIT NONE
INTEGER I, INDEX(5), ISEED, J, NOUT, NPOP, NROW, NVAR
REAL POP(176,2), SAMP(5,2)
!
CALL UMACH (2, NOUT)
! Get Wolfer sunspot data to use
! as “population”.
CALL GDATA (2, POP, NROW, NVAR)
! Initialize seed of random number
! generator.
ISEED = 123457
CALL RNSET (ISEED)
CALL RNSRS (POP, NPOP, SAMP, INDEX)
WRITE (NOUT,99999) NPOP, INDEX, ((SAMP(I,J),I=1,5),J=1,2)
99999 FORMAT (' The population size is ', I5, /, ' Indices of ', &
'random sample: ', 5I8, /, ' The sample: ' &
, 5F8.0, /, ' ', 5F8.0)
END
Output
 
The population size is 176
Indices of random sample: 16 80 175 25 21
The sample: 1764. 1828. 1923. 1773. 1769.
36. 62. 6. 35. 106.
Example 2
Routine RNSRS 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 RNSRS one observation at a time. This is the way RNSRS may be used to sample from a file on disk or tape. Notice that the number of records need not be known in advance.
 
USE RNSRS_INT
USE UMACH_INT
USE GDATA_INT
USE RNSET_INT
 
IMPLICIT NONE
INTEGER ISEED, NOUT, IDO, NROW, NVAR, NPOP, INDEX(5), I, J
REAL POP(176,2), SAMP(5,2), X(2, 1)
CALL UMACH(2, NOUT)
! Get Wolfer sunspot data to use
! as “population”.
CALL GDATA (2, POP, NROW, NVAR)
! Initialize seed of random number
! generator.
ISEED = 123457
CALL RNSET(ISEED)
IDO = 1
DO 10 I=1,176
! In this DO-loop, the data would
! generally be read from a file,
! one observation at a time. This
! program simulates this by copying
! the observations one at a time into
! X from POP.
X(1,1) = POP(I,1)
X(2,1) = POP(I,2)
CALL RNSRS (X, NPOP, SAMP, INDEX, IDO=IDO, &
NROW=1, NVAR=NVAR, LDPOP=1)
IDO = 2
10 CONTINUE
WRITE(NOUT, 20) NPOP, INDEX, ((SAMP(I,J),I=1,5),J=1,2)
20 FORMAT (' The population size is ', I5,/, &
' Indices of random sample: ', 5I8,/, &
' The sample: ', 5F8.0,/, &
' ', 5F8.0)
END
Output
 
The population size is 176
Indices of random sample: 16 80 175 25 21
The sample: 1764. 1828. 1923. 1773. 1769.
36. 62. 6. 35. 106.
Published date: 03/19/2020
Last modified date: 03/19/2020