// A few common random functions. (1.02)
#include <algorithm>
#include <experimental/iterator>
#include <random>
#include <iostream>
using namespace std;
static thread_local default_random_engine re_(random_device{}());
// Real in the range [0, 1).
double randreal() {
uniform_real_distribution<double> pick(0, 1);
return pick(re_);
}
// Integer in the range [lo, hi].
int randint(int lo, int hi) {
uniform_int_distribution<> pick(lo, hi);
return pick(re_);
}
// Boolean where probability of true is p and false is (1-p).
bool randbool(double p) {
bernoulli_distribution pick(p);
return pick(re_);
}
// Show.
template<typename Func, typename... Args>
void show(int n, Func f, Args... args) {
generate_n(experimental::make_ostream_joiner(cout, " "), n,
[=]{ return f(args...); });
cout << endl;
}
int main() {
show(10, randreal);
show(10, randint, -5, 5);
show(10, randbool, 0.5);
}
Ly8gQSBmZXcgY29tbW9uIHJhbmRvbSBmdW5jdGlvbnMuICgxLjAyKQoKI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGV4cGVyaW1lbnRhbC9pdGVyYXRvcj4KI2luY2x1ZGUgPHJhbmRvbT4KI2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKc3RhdGljIHRocmVhZF9sb2NhbCBkZWZhdWx0X3JhbmRvbV9lbmdpbmUgcmVfKHJhbmRvbV9kZXZpY2V7fSgpKTsKCi8vIFJlYWwgaW4gdGhlIHJhbmdlIFswLCAxKS4KZG91YmxlIHJhbmRyZWFsKCkgewogICAgdW5pZm9ybV9yZWFsX2Rpc3RyaWJ1dGlvbjxkb3VibGU+IHBpY2soMCwgMSk7CiAgICByZXR1cm4gcGljayhyZV8pOwp9CgovLyBJbnRlZ2VyIGluIHRoZSByYW5nZSBbbG8sIGhpXS4KaW50IHJhbmRpbnQoaW50IGxvLCBpbnQgaGkpIHsKICAgIHVuaWZvcm1faW50X2Rpc3RyaWJ1dGlvbjw+IHBpY2sobG8sIGhpKTsKICAgIHJldHVybiBwaWNrKHJlXyk7Cn0KCi8vIEJvb2xlYW4gd2hlcmUgcHJvYm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