1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <iostream>
#include <random>
#include <ctime>
#include <map>
int main()
{
std::random_device rd;
std::mt19937 gen(time(nullptr) /*rd()*/);
std::uniform_real_distribution<> random_norm(0.0, 1.0);
// 1, 2, 3, 4, 5, 6, // 6/20 = 30%
// 7, 8, 9, 10, 11, 12, // 6/20 = 30%
// 13, 14, 15, 16, 17, 18, // 6/20 = 30%
// 19, // 1/20 = 5%
// 20 // 1/20 = 5%
//
int outputs[20] = { 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4,
5, 8 };
std::map<int, int> output_count;
const int iterations = 10000;
for (int i = 0; i < iterations; i++)
{
double r_in = random_norm(gen); // 20 * [0.0, 1.0) == [0.0, 20.0)
int r_out = outputs[static_cast<int>(20 * r_in)];
output_count[r_out]++;
}
for (auto& pair : output_count)
{
std::cout << pair.first // (key)
<< ':'
<< (static_cast<double>(pair.second) / iterations) * 100.0 // (value as frequency percentage)
<< '\n';
}
}
|