In statistical terms, population sampling involves choosing from a vast set of items called population. A representative set of items is called the sample population with a defined size known as the sample size. This selection could be random that is unbiased or biased.
If we have a sampling situation requiring an unbiased sampling, we use the sample() method.
The sample() method will select a set of random samples from a population. While making this selection, the selected items at any point in time may pop out of the population or be recycled back into the population. If popped out, it’s known as selection without replacement. But if recycled, it’s the selection with replacement method.
While the without replacement method offers every item in the population only one chance of selection during sampling, the with replacement method offers multiple chances of choice for each item.
sample(population, sample_size, sampling_method)
population: A set of items from which a sample will choose.
sample_size: This is a number of the population items we aim to sample. It is an integer value and a required parameter.
sampling_method: This integer value indicates the method we want to employ in making our samples. It has the following possible values:
sampling_method < 0: This value indicates that a selection with-replacement method will be employed.
sampling_method = 0: This value indicates that a selection without-replacement method will be employed.
sampling_method > 0: This value indicates that a selection without-replacement method will be employed, and the chosen items that are the samples will be returned alongside the items that were not selected.
We’ll have the following as the return values:
0 or less than 00, the sample will be returned alongside the items that were not selected. That is, two sequence values will be returned.include std/rand.e--without replacement samplingprintf(1, "without replacement: %s\n", { sample("afghibcdetuvwxyzjklmnopqrs", 1)})printf(1, "sample size of five :%s\n", { sample("abcdefghijklmnopqrstuvwxyz", 5)})printf(1, "negative (-1) sample size: %s\n", { sample("afghibcdetuvwxyzjklmnopqrs", -1)}) --> ""--with replacement samplingprintf(1, "with replacement : %s\n", { sample("ijklmnopqrsabcdefghtuvwxyz", 1, -1)})printf(1, "negative (-1) sample size: %s\n", { sample("abcdefghijklmnopqrstuvwxyz", -1, -1)})printf(1, "here sample size is larger than population :%s\n", { sample("abcdefghijklmnopqrstuvwxyz", 50, -1)})
When the sampling size is negative, the return value is empty.
Line 2: Include the std/rand.e module.
Line 4–9: We do some sampling using the without-replacement selection method.
Line 13–17: We do some sampling using the with replacement method. This method can specify a sample size more significantly than the population size.