#include <iostream>
#include <random>
#include <map>
#include <iomanip>
int keep_out_of_range(int low, int high, int number)
{
if (number >= low && number <= high)
{
int halfway = (low + high) / 2;
int halfrange = (high - low) / 2;
if (number > halfway)
number += halfrange;
else
{
number -= halfrange + 1;
}
}
return number;
}
int main()
{
const int MinGen = -100;
const int MaxGen = 100;
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(MinGen, MaxGen);
const int nrolls = 10000; // number of experiments
// generate numbers, make a histogram of distribution
std::map<int, int> histogram;
for (int i = 0; i < nrolls; i++) {
int random_number = distribution(generator); // here's where # is generated
random_number = keep_out_of_range(10, 20, random_number);
++histogram[random_number];
}
// print the histogram
for (auto p : histogram) {
std::cout << std::fixed << std::setprecision(1) << std::setw(2)
<< p.first << ' ' << std::string(p.second/(nrolls/(10*(MaxGen-MinGen))), '*') << '\n';
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8cmFuZG9tPgoKI2luY2x1ZGUgPG1hcD4KI2luY2x1ZGUgPGlvbWFuaXA+CgppbnQga2VlcF9vdXRfb2ZfcmFuZ2UoaW50IGxvdywgaW50IGhpZ2gsIGludCBudW1iZXIpCnsKCWlmIChudW1iZXIgPj0gbG93ICYmIG51bWJlciA8PSBoaWdoKQoJewoJCWludCBoYWxmd2F5ID0gKGxvdyArIGhpZ2gpIC8gMjsKCQlpbnQgaGFsZnJhbmdlID0gKGhpZ2ggLSBsb3cpIC8gMjsKCQlpZiAobnVtYmVyID4gaGFsZndheSkKCQkJbnVtYmVyICs9IGhhbGZyYW5nZTsKCQllbHNlCgkJewoJCQludW1iZXIgLT0gaGFsZnJhbmdlICsgMTsKCQl9Cgl9CglyZXR1cm4gbnVtYmVyOwp9CgppbnQgbWFpbigpCnsKICBjb25zdCBpbnQgTWluR2VuID0gLTEwMDsKICBjb25zdCBpbnQgTWF4R2VuID0gIDEwMDsKICBzdGQ6OmRlZmF1bHRfcmFuZG9tX2VuZ2luZSBnZW5lcmF0b3I7CiA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