// Most common random functions. (2.00)
#include <random>
#include <iostream>
using namespace std;
class Random {
static thread_local default_random_engine s_re;
public:
// Real in the range [0, 1) (exclusive).
static double randreal() {
uniform_real_distribution<double> pick(0.0, 1.0);
return pick(s_re);
}
// Integer in the range [lo, hi] (inclusive).
static int randint(int lo, int hi) {
uniform_int_distribution<> pick(lo, hi);
return pick(s_re);
}
// Boolean with probability {p true | (1-p) false}.
static bool randbool(double p) {
bernoulli_distribution pick(p);
return pick(s_re);
}
};
thread_local default_random_engine Random::s_re(random_device{}());
// Show.
int main() {
int n = 10;
for (int i = 0; i < n; i++)
cout << Random::randreal() << ' ';
cout << endl;
for (int i = 0; i < n; i++)
cout << Random::randint(-n/2, n/2) << ' ';
cout << endl;
for (int i = 0; i < n; i++)
cout << Random::randbool(0.5) << ' ';
cout << endl;
}
Ly8gTW9zdCBjb21tb24gcmFuZG9tIGZ1bmN0aW9ucy4gKDIuMDApCgojaW5jbHVkZSA8cmFuZG9tPgojaW5jbHVkZSA8aW9zdHJlYW0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpjbGFzcyBSYW5kb20gewogICAgc3RhdGljIHRocmVhZF9sb2NhbCBkZWZhdWx0X3JhbmRvbV9lbmdpbmUgc19yZTsKCnB1YmxpYzoKICAgIC8vIFJlYWwgaW4gdGhlIHJhbmdlIFswLCAxKSAoZXhjbHVzaXZlKS4KICAgIHN0YXRpYyBkb3VibGUgcmFuZHJlYWwoKSB7CiAgICAgICAgdW5pZm9ybV9yZWFsX2Rpc3RyaWJ1dGlvbjxkb3VibGU+IHBpY2soMC4wLCAxLjApOwogICAgICAgIHJldHVybiBwaWNrKHNfcmUpOwogICAgfQoKICAgIC8vIEludGVnZXIgaW4gdGhlIHJhbmdlIFtsbywgaGldIChpbmNsdXNpdmUpLgogICAgc3RhdGljIGludCByYW5kaW50KGludCBsbywgaW50IGhpKSB7CiAgICAgICAgdW5pZm9ybV9pbnRfZGlzdHJpYnV0aW9uPD4gcGljayhsbywgaGkpOwogICAgICAgIHJldHVybiBwaWNrKHNfcmUpOwogICAgfQoKICAgIC8vIEJvb2xlYW4gd2l0aCBwcm9iYWJpbGl0eSB7cCB0cnVlIHwgKDEtcCkgZmFsc2V9LgogICAgc3RhdGljIGJvb2wgcmFuZGJvb2woZG91YmxlIHApIHsKICAgICAgICBiZXJub3VsbGlfZGlzdHJpYnV0aW9uIHBpY2socCk7CiAgICAgICAgcmV0dXJuIHBpY2soc19yZSk7CiAgICB9Cn07Cgp0aHJlYWRfbG9jYWwgZGVmYXVsdF9yYW5kb21fZW5naW5lIFJhbmRvbTo6c19yZShyYW5kb21fZGV2aWNle30oKSk7CgovLyBTaG93LgoKaW50IG1haW4oKSB7CiAgICBpbnQgbiA9IDEwOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspCiAgICAgICAgY291dCA8PCBSYW5kb206OnJhbmRyZWFsKCkgPDwgJyAnOwogICAgY291dCA8PCBlbmRsOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspCiAgICAgICAgY291dCA8PCBSYW5kb206OnJhbmRpbnQoLW4vMiwgbi8yKSA8PCAnICc7CiAgICBjb3V0IDw8IGVuZGw7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykKICAgICAgICBjb3V0IDw8IFJhbmRvbTo6cmFuZGJvb2woMC41KSA8PCAnICc7CiAgICBjb3V0IDw8IGVuZGw7Cn0=