fork(1) download
  1. // A few common random functions. (1.01)
  2.  
  3. #include <random>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. static thread_local default_random_engine re_(random_device{}());
  8.  
  9. // Real in the range [0, 1).
  10. double randreal() {
  11. uniform_real_distribution<double> pick(0, 1);
  12. return pick(re_);
  13. }
  14.  
  15. // Integer in the range [lo, hi].
  16. int randint(int lo, int hi) {
  17. uniform_int_distribution<> pick(lo, hi);
  18. return pick(re_);
  19. }
  20.  
  21. // Boolean where probability of true is p and false is (1-p).
  22. bool randbool(double p) {
  23. bernoulli_distribution pick(p);
  24. return pick(re_);
  25. }
  26.  
  27. // Show.
  28.  
  29. template<typename Func, typename... Args>
  30. void show(int n, Func f, Args... args) {
  31. for (int i = 0; i < n; i++)
  32. cout << f(args...) << ' ';
  33. cout << endl;
  34. }
  35.  
  36. int main() {
  37. show(10, randreal);
  38. show(10, randint, -5, 5);
  39. show(10, randbool, 0.5);
  40. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
0.00191772 0.573692 0.123948 0.45683 0.665049 0.994188 0.576176 0.665915 0.241909 0.294008 
-1 -4 1 -5 -2 5 -5 3 -3 -5 
0 0 1 0 0 1 1 1 0 0