Created
February 18, 2020 12:48
-
-
Save lnikon/78c80f05c054100509be9a42db609839 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <map> | |
#include <string> | |
#include <tuple> | |
#include <iterator> | |
#include <algorithm> | |
#include <numeric> | |
#include <random> | |
#include <iomanip> | |
using namespace std; | |
namespace std { | |
ostream& operator<<(ostream& os, const pair<int, string>& p) | |
{ | |
return os << "(" << p.first << ", " << p.second << ")"; | |
} | |
template <typename T> | |
ostream& operator<<(ostream& os, const vector<T>& vi) | |
{ | |
os << "{ "; | |
for (const auto& i: vi) | |
{ | |
os << i << ", "; | |
} | |
os << "}\n"; | |
return os; | |
} | |
} | |
int int_generator() | |
{ | |
static int i = 0; | |
return i++; | |
} | |
int main() | |
{ | |
constexpr const size_t data_points = 100000; | |
constexpr const size_t sample_points = 100; | |
constexpr const size_t mean = 10; | |
constexpr const size_t dev = 3; | |
random_device rd; | |
mt19937 gen{rd()}; | |
normal_distribution<> d{mean, dev}; | |
std::vector<int> v; | |
v.reserve(data_points); | |
generate_n(back_inserter(v), data_points, [&] { return d(gen); }); | |
std::vector<int> samples; | |
samples.reserve(sample_points); | |
sample(begin(v), end(v), back_inserter(samples), | |
sample_points, mt19937{random_device{}()}); | |
map<int, char> hist; | |
for (int i : samples) { ++hist[i]; } | |
for (const auto& [value, count] : hist) | |
{ | |
cout << setw(2) << value << " " | |
<< string(count, '*') << '\n'; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment