// A few common random functions. (1.03)
#include <algorithm>
#include <experimental/iterator>
#include <random>
#include <iostream>
using namespace std;
// Initialize generator with non-deterministic seed.
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_);
}
// Main.
template<typename Func, typename... Args>
void display(int n, Func f, Args... args)
{
cout << '[';
generate_n(experimental::make_ostream_joiner(cout, ", "), n,
[=]{ return f(args...); });
cout << "]\n";
}
int main()
{
display(10, randreal);
display(10, randint, -5, 5);
display(10, randbool, 0.5);
}
Ly8gQSBmZXcgY29tbW9uIHJhbmRvbSBmdW5jdGlvbnMuICgxLjAzKQoKI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGV4cGVyaW1lbnRhbC9pdGVyYXRvcj4KI2luY2x1ZGUgPHJhbmRvbT4KI2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLy8gSW5pdGlhbGl6ZSBnZW5lcmF0b3Igd2l0aCBub24tZGV0ZXJtaW5pc3RpYyBzZWVkLgoKc3RhdGljIHRocmVhZF9sb2NhbCBkZWZhdWx0X3JhbmRvbV9lbmdpbmUgcmVfKHJhbmRvbV9kZXZpY2V7fSgpKTsKCi8vIFJlYWwgaW4gdGhlIHJhbmdlIFswLCAxKS4KCmRvdWJsZSByYW5kcmVhbCgpCnsKICAgIHVuaWZvcm1fcmVhbF9kaXN0cmlidXRpb248ZG91YmxlPiBwaWNrKDAsIDEpOwogICAgcmV0dXJuIHBpY2socmVfKTsKfQoKLy8gSW50ZWdlciBpbiB0aGUgcmFuZ2UgW2xvLCBoaV0uCgppbnQgcmFuZGludChpbnQgbG8sIGludCBoaSkKewogICAgdW5pZm9ybV9pbnRfZGlzdHJpYnV0aW9uPD4gcGljayhsbywgaGkpOwogICAgcmV0dXJuIHBpY2socmVfKTsKfQoKLy8gQm9vbGVhbiB3aGVyZSBwcm9iYWJpbGl0eSBvZiB0cnVlIGlzIHAgYW5kIGZhbHNlIGlzICgxLXApLgoKYm9vbCByYW5kYm9vbChkb3VibGUgcCkKewogICAgYmVybm91bGxpX2Rpc3RyaWJ1dGlvbiBwaWNrKHApOwogICAgcmV0dXJuIHBpY2socmVfKTsKfQoKLy8gTWFpbi4KCnRlbXBsYXRlPHR5cGVuYW1lIEZ1bmMsIHR5cGVuYW1lLi4uIEFyZ3M+CnZvaWQgZGlzcGxheShpbnQgbiwgRnVuYyBmLCBBcmdzLi4uIGFyZ3MpCnsKICAgIGNvdXQgPDwgJ1snOwogICAgZ2VuZXJhdGVfbihleHBlcmltZW50YWw6Om1ha2Vfb3N0cmVhbV9qb2luZXIoY291dCwgIiwgIiksIG4sCiAgICAgICAgICAgICAgIFs9XXsgcmV0dXJuIGYoYXJncy4uLik7IH0pOwogICAgY291dCA8PCAiXVxuIjsKfQoKaW50IG1haW4oKQp7CiAgICBkaXNwbGF5KDEwLCByYW5kcmVhbCk7CiAgICBkaXNwbGF5KDEwLCByYW5kaW50LCAtNSwgNSk7CiAgICBkaXNwbGF5KDEwLCByYW5kYm9vbCwgMC41KTsKfQ==
[0.563179, 0.454795, 0.124613, 0.726201, 0.191695, 0.667348, 0.528444, 0.851115, 0.456227, 0.997265]
[-5, -4, 1, -1, 0, -3, 5, 5, -2, 0]
[1, 1, 1, 0, 0, 0, 0, 1, 1, 0]