// A few common random functions. (1.05)
#include <random>
#include <limits>
#include <algorithm>
#include <experimental/iterator>
#include <iostream>
using namespace std;
// Initialize generator with non-deterministic seed.
static thread_local default_random_engine re_(random_device{}());
// Real uniformly distributed on the interval [0, 1).
double randreal()
{
return generate_canonical<double, numeric_limits<double>::digits>(re_);
}
// Integer uniformly distributed on the closed interval [a, b].
int randint(int a, int b)
{
uniform_int_distribution<> pick(min(a, b), max(a, b));
return pick(re_);
}
// Boolean where the probability of true is p and false is (1-p).
bool randbool(double p)
{
bernoulli_distribution pick(min(max(p, 0.0), 1.0));
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);
}
Ly8gQSBmZXcgY29tbW9uIHJhbmRvbSBmdW5jdGlvbnMuICgxLjA1KQoKI2luY2x1ZGUgPHJhbmRvbT4KI2luY2x1ZGUgPGxpbWl0cz4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGV4cGVyaW1lbnRhbC9pdGVyYXRvcj4KI2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLy8gSW5pdGlhbGl6ZSBnZW5lcmF0b3Igd2l0aCBub24tZGV0ZXJtaW5pc3RpYyBzZWVkLgoKc3RhdGljIHRocmVhZF9sb2NhbCBkZWZhdWx0X3JhbmRvbV9lbmdpbmUgcmVfKHJhbmRvbV9kZXZpY2V7fSgpKTsKCi8vIFJlYWwgdW5pZm9ybWx5IGRpc3RyaWJ1dGVkIG9uIHRoZSBpbnRlcnZhbCBbMCwgMSkuCgpkb3VibGUgcmFuZHJlYWwoKQp7CiAgICByZXR1cm4gZ2VuZXJhdGVfY2Fub25pY2FsPGRvdWJsZSwgbnVtZXJpY19saW1pdHM8ZG91YmxlPjo6ZGlnaXRzPihyZV8pOwp9CgovLyBJbnRlZ2VyIHVuaWZvcm1seSBkaXN0cmlidXRlZCBvbiB0aGUgY2xvc2VkIGludGVydmFsIFthLCBiXS4KCmludCByYW5kaW50KGludCBhLCBpbnQgYikKewogICAgdW5pZm9ybV9pbnRfZGlzdHJpYnV0aW9uPD4gcGljayhtaW4oYSwgYiksIG1heChhLCBiKSk7CiAgICByZXR1cm4gcGljayhyZV8pOwp9CgovLyBCb29sZWFuIHdoZXJlIHRoZSBwcm9iYWJpbGl0eSBvZiB0cnVlIGlzIHAgYW5kIGZhbHNlIGlzICgxLXApLgoKYm9vbCByYW5kYm9vbChkb3VibGUgcCkKewogICAgYmVybm91bGxpX2Rpc3RyaWJ1dGlvbiBwaWNrKG1pbihtYXgocCwgMC4wKSwgMS4wKSk7CiAgICByZXR1cm4gcGljayhyZV8pOwp9CgovLyBNYWluLgoKdGVtcGxhdGU8dHlwZW5hbWUgRnVuYywgdHlwZW5hbWUuLi4gQXJncz4Kdm9pZCBkaXNwbGF5KGludCBuLCBGdW5jIGYsIEFyZ3MuLi4gYXJncykKewogICAgY291dCA8PCAnWyc7CiAgICBnZW5lcmF0ZV9uKGV4cGVyaW1lbnRhbDo6bWFrZV9vc3RyZWFtX2pvaW5lcihjb3V0LCAiLCAiKSwgbiwKICAgICAgICAgICAgICAgWz1deyByZXR1cm4gZihhcmdzLi4uKTsgfSk7CiAgICBjb3V0IDw8ICJdXG4iOwp9CgppbnQgbWFpbigpCnsKICAgIGRpc3BsYXkoMTAsIHJhbmRyZWFsKTsKICAgIGRpc3BsYXkoMTAsIHJhbmRpbnQsIC01LCA1KTsKICAgIGRpc3BsYXkoMTAsIHJhbmRib29sLCAwLjUpOwp9
[0.766086, 0.947016, 0.940326, 0.256573, 0.446622, 0.387364, 0.568334, 0.874144, 0.423408, 0.4222]
[5, 0, 4, 5, -3, -5, -3, 2, 4, -5]
[1, 0, 1, 1, 0, 0, 1, 0, 0, 1]