DM560, Introduction to Programming in C++
Sheet 13
Task 3
#include <chrono>
#include <iostream>
#include <random>
#include <tuple>
#include <map>
#include <iomanip>
#include <string>
using namespace std;
class Rand_int {
public:
Rand_int(double lambda, int low, int high, unsigned seed = 0)
: re{seed}, dist_a{lambda}, dist_f{low, high} {};
tuple<double, int, int> operator()() {
return std::make_tuple(dist_a(re), dist_f(re), dist_f(re));
}
private:
default_random_engine re; // generates random numbers
exponential_distribution<> dist_a;
uniform_int_distribution<> dist_f;
};
int main() {
int nseconds = 100;
unsigned seed = chrono::system_clock::now()
.time_since_epoch()
.count(); // clock resolution
Rand_int r(2, 0, 5, seed); // lambda: number of expected arrival per nseconds seconds
double interarrival; // = std::get<0>(triple);
int floor_arrival; // = std::get<1>(triple);
int floor_destination; // = std::get<2>(triple);
cout << interarrival << " " << floor_arrival << " " << floor_destination
<< endl;
std::map<int, int> hist;
for (int n = 0; n < 10000; ++n) {
tie(interarrival, floor_arrival, floor_destination) = r();
if (interarrival<1) ++hist[round(nseconds*interarrival)];
}
for (auto p : hist) {
cout << fixed << setprecision(1) << setw(2) << p.first << ' '
<< string(p.second/5, '*') << "\n";
}
}