fork(1) download
  1. // Most common random functions. (1.00)
  2.  
  3. #include <random>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. thread_local default_random_engine s_re(random_device{}());
  8.  
  9. // Real in the range [0, 1) (exclusive).
  10. double randreal() {
  11. uniform_real_distribution<double> pick(0.0, 1.0);
  12. return pick(s_re);
  13. }
  14.  
  15. // Integer in the range [lo, hi] (inclusive).
  16. int randint(int lo, int hi) {
  17. uniform_int_distribution<> pick(lo, hi);
  18. return pick(s_re);
  19. }
  20.  
  21. // Boolean with probability {p true | (1-p) false}.
  22. bool randbool(double p) {
  23. bernoulli_distribution pick(p);
  24. return pick(s_re);
  25. }
  26.  
  27. // Show.
  28.  
  29. int main() {
  30. int n = 10;
  31. for (int i = 0; i < n; i++)
  32. cout << randreal() << ' ';
  33. cout << endl;
  34. for (int i = 0; i < n; i++)
  35. cout << randint(-n/2, n/2) << ' ';
  36. cout << endl;
  37. for (int i = 0; i < n; i++)
  38. cout << randbool(0.5) << ' ';
  39. cout << endl;
  40. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
0.356388 0.691842 0.421792 0.342169 0.732448 0.121107 0.289215 0.179304 0.856747 0.478543 
4 -3 -5 -5 4 5 2 5 0 1 
1 1 0 0 1 0 0 1 1 0