Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Pseudo-random number generation
The random number library provides classes that generate random and pseudo-random numbers. The library contains two types of components:
- Engines, which are generators of random numbers (both pseudo-random number generators, which generate integer sequences with a uniform distribution, and true random number generators if available)
- Distributions which convert the output of random number engines into various statistical distributions
Random number engines
All engines (except the random_device
) produce integer numbers in a uniform distribution using seed data as entropy source.
Random number distributions
Random number distributions post-processe the output of an random number engine in such a way that the resulting output is distributed according to a defined statistical probability density function.
Example
The follwing example prints the uniform distribution of an random generator.
TODO
Play around with different engines and distributions.
play around with different engines and distributions.
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
44
45
46
47
48
49
50
51
#include <functional>
#include <map>
#include <algorithm>
#include <iostream>
#include <random>
#include <iomanip>
// Taken from "Modern C++ Programming Cookbook" by Marius Bancila
void generate_and_print(std::function<int()> rnd_generator,
const int iterations = 10000) {
auto data = std::map<int, int>{};
// generate random numbers
for (auto n = 0; n < iterations; ++n) {
++data[rnd_generator()];
}
// find element with the most repetitions
const auto& [_, max_rep] = *std::max_element(
std::begin(data),
std::end(data),
[](const auto value_rep_pair_1, const auto& value_rep_pair_2) {
return value_rep_pair_1.second < value_rep_pair_2.second;
});
// print bars
constexpr char block_character= 219;
const auto rep_unit_width = iterations / 50;
for (auto i = max_rep / rep_unit_width; i > 0; --i) {
for (const auto& [val, rep] : data) {
std::cout << std::fixed
<< std::setprecision(1)
<< std::setw(3)
<< ((rep / rep_unit_width) >= i ? block_character : ' ');
}
std::cout << "\n";
}
// print numbers
for (const auto& [val, rep] : data) {
std::cout << std::fixed
<< std::setprecision(1)
<< std::setw(3)
<< val;
}
std::cout << "\n";
};
int main() {
std::random_device rd{};
// TODO play around with different engines.
auto mtgen = std::mt19937{rd()};
// TODO play around with different distributions.
Enter to Rename, Shift+Enter to Preview
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content