forked from tcbrindle/flux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistogram.cpp
61 lines (50 loc) · 1.47 KB
/
histogram.cpp
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
61
// Copyright (c) 2023 Jiri Nytra (jiri.nytra at gmail dot com)
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <flux.hpp>
#include <cstddef>
#include <ostream>
#include <string>
#include <random>
#include <iostream>
#include <iomanip>
#include <map>
template<typename T>
flux::generator<int> randu(T min, T max)
{
std::mt19937 rng(std::random_device{}());
std::uniform_int_distribution dist(min, max);
while (true) {
co_yield dist(rng);
}
}
template<typename T>
flux::generator<int> randn(T mean, T stddev = 1.0)
{
std::mt19937 rng(std::random_device{}());
std::normal_distribution dist(mean, stddev);
while (true) {
co_yield static_cast<int>(std::round(dist(rng)));
}
}
using hist_t = std::map<int, int>;
auto to_histogram = [](hist_t&& so_far, auto x)
{
++so_far[x];
return std::move(so_far);
};
void print_histogram(const hist_t& hist)
{
for (auto [bin, count]: hist){
std::cout << std::setw(2) << bin << ' ' << std::string(std::size_t(count)/200, '*') << '\n';
}
};
int main()
{
std::cout << "Uniform distribution from 0 to 10\n";
print_histogram(randu(0, 10).take(10000).fold(to_histogram, hist_t{}));
std::cout << '\n';
std::cout << "Normal distribution with mean 5 and stddev 2\n";
print_histogram(randn(5.0, 2.0).take(10000).fold(to_histogram, hist_t{}));
std::cout << '\n';
}