fork(1) download
  1. // A few common random functions. (1.02)
  2.  
  3. #include <algorithm>
  4. #include <experimental/iterator>
  5. #include <random>
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. static thread_local default_random_engine re_(random_device{}());
  10.  
  11. // Real in the range [0, 1).
  12. double randreal() {
  13. uniform_real_distribution<double> pick(0, 1);
  14. return pick(re_);
  15. }
  16.  
  17. // Integer in the range [lo, hi].
  18. int randint(int lo, int hi) {
  19. uniform_int_distribution<> pick(lo, hi);
  20. return pick(re_);
  21. }
  22.  
  23. // Boolean where probability of true is p and false is (1-p).
  24. bool randbool(double p) {
  25. bernoulli_distribution pick(p);
  26. return pick(re_);
  27. }
  28.  
  29. // Show.
  30.  
  31. template<typename Func, typename... Args>
  32. void show(int n, Func f, Args... args) {
  33. generate_n(experimental::make_ostream_joiner(cout, " "), n,
  34. [=]{ return f(args...); });
  35. cout << endl;
  36. }
  37.  
  38. int main() {
  39. show(10, randreal);
  40. show(10, randint, -5, 5);
  41. show(10, randbool, 0.5);
  42. }
Success #stdin #stdout 0s 5268KB
stdin
Standard input is empty
stdout
0.211626 0.81638 0.45515 0.218571 0.592045 0.322514 0.975205 0.254163 0.947285 0.07257
2 4 -4 -2 5 -4 -3 0 -3 -2
1 0 0 1 1 1 1 0 0 0