With C ++ 11, there are many new options for creating non-uniform pseudorandom numbers in the random header . The code example below demonstrates possible non-uniform distributions:
#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <random>
int main()
{
std::random_device rd;
std::mt19937 e2(rd());
std::normal_distribution<> dist(2, 2);
std::map<int, int> hist;
for (int n = 0; n < 10000; ++n) {
++hist[std::round(dist(e2))];
}
for (auto p : hist) {
std::cout << std::fixed << std::setprecision(1) << std::setw(2)
<< p.first << ' ' << std::string(p.second/200, '*') << '\n';
}
}
using the normal distribution, you will see output similar to this:
-5
-4
-3
-2 *
-1 ***
0 ******
1 ********
2 *********
3 ********
4 ******
5 ***
6 *
7
8
9
source
share