Skip to content

Instantly share code, notes, and snippets.

@lnikon
Created February 18, 2020 12:48
Show Gist options
  • Save lnikon/78c80f05c054100509be9a42db609839 to your computer and use it in GitHub Desktop.
Save lnikon/78c80f05c054100509be9a42db609839 to your computer and use it in GitHub Desktop.
#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