#include <iostream>
#include <random>
#include <vector>
std::uniform_int_distribution<int> uni_int_dist_0_9{0, 9};
std::default_random_engine random_engine{};
const int NbrOfBins = 10;
const int NbrOfSamples = 100;
int main()
{
std::vector<int> histogram(NbrOfBins);
int sampleValue;
for (int sample = 0; sample < NbrOfSamples; ++sample)
{
sampleValue = uni_int_dist_0_9(random_engine);
++histogram[sampleValue];
}
for (int bin = 0; bin < NbrOfBins; ++bin)
{
std::cout << histogram[bin] << std::endl;
}
return 0;
}
/* Output:
10
6
11
10
9
9
16
6
8
15
*/
Reference: https://isocpp.org/wiki/faq/cpp11-library#std-random
Friday, September 1, 2017
C++11: Random Numbers
C++11 added more flexibility in generating random numbers. The system is divided into two parts: 1) A random number generator; and 2) a distribution. Each one can be set separately. Here is an example:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment