Sample data sets with std::sample
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,000 random integers, with standard normal distribution. 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 adouble
. The standard library lacks such a function and we'll need it later:int iround(const double& d) { return static_cast<int>(std::round(d)); }
The standard...