What is the sample() method in Euphoria?

Overview

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.

Syntax

sample(population, sample_size, sampling_method)

Parameters

  • 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.

Return value

We’ll have the following as the return values:

  1. A sequence containing the selected items that are the sample. We only get this when sampling_method is 0 or less than 0
  2. But if the sampling_method parameter has a value greater than 0, the sample will be returned alongside the items that were not selected. That is, two sequence values will be returned.

Code

include std/rand.e
--without replacement sampling
printf(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 sampling
printf(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)})

Explanation

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.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources