Sample Data Sets with std::sample

Learn to sample data sets with std::sample.

We'll cover the following

The std::sample() algorithm takes a random sample of a sequence of values and populates a destination container with the sample. It is useful for analyzing a larger set of data, where the random sample is taken to be representative of the whole.

A sample set allows us to approximate the characteristics of a large set of data, without analyzing the full set. This provides efficiency in exchange for accuracy, a fair trade-off in many circumstances.

How to do it

In this recipe, we'll use an array of 200,000200,000 random integers, with standard normal distributionStandard normal distribution refers to a specific probability distribution with a mean of 0 and a standard deviation of 1, often used as a baseline for statistical analysis and hypothesis testing.. We'll sample a few hundred values to create a histogram of the frequency of each value.

  • We'll start with a simple function to return a rounded int from a double. The standard library lacks such a function and we'll need it later:

Get hands-on with 1400+ tech skills courses.