-
Notifications
You must be signed in to change notification settings - Fork 0
/
06-random.cc
60 lines (52 loc) · 1.58 KB
/
06-random.cc
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
52
53
54
55
56
57
58
59
60
//
// Program
// Random numbers: Generate random numbers b/w different ranges as
// per their defined weights linearly.
//
// Compile
// g++ -Wall -Wextra -pedantic -std=c++17 -o 06-random 06-random.cc
//
// Execution
// ./06-random
//
#include <iostream>
#include <random>
#include <algorithm>
#include <map>
#include <iomanip>
constexpr int scale = 1000;
constexpr int limit = 100000;
static void show_histogram(const std::string& title, const std::map<int, int> &hist) {
std::cout << "--- " << title << " ---" << '\n';
for (auto item: hist) {
std::cout << " " << std::setw(2) << item.first << " "
<< std::string(item.second/scale, '*')
<< " (" << item.second << " times)"
<< '\n';
}
}
//
// Entry function
//
int main() {
{
std::map<int, int> hist{};
std::random_device rd {};
std::mt19937 mt(rd()); // Get seed from random_device
//
// As per weight, we are looking following distribution of our rand numbers:
// - 1.0 <> 5.0 -> increase value linearly
// - 5.0 <> 10.0 -> remains constant
// - 10.0 <> 15.0 -> decrease value linearly
std::vector<double> input {1.0, 5.0, 10.0, 15.0};
std::vector<double> weight{0.0, 0.5, 0.5, 0.0};
std::piecewise_linear_distribution<double> pcd(std::begin(input),
std::end(input),
std::begin(weight));
for (int i = 0; i <= limit; ++i) {
hist[pcd(mt)] += 1;
}
show_histogram("piecewise_linear_distribution with mt19937", hist);
}
return 0;
}