fork download
  1. #include <random> //Random stuff
  2. #include <ctime> //std::time
  3. #include <iostream>
  4.  
  5. int rand_num(int from, int to)
  6. {
  7. //Static is needed to seed our prng only once
  8. //and all function calls will share generator
  9. static std::mt19937 rg(std::time(nullptr));
  10. //distribution is recreated each call because function might be called with different parameters
  11. std::uniform_int_distribution<> dis(from, to);
  12. return dis(rg); //return result of generation
  13. }
  14.  
  15. int main()
  16. {
  17. for(int i =0; i < 20; ++i)
  18. std::cout << rand_num(1, 100) << '\n';
  19. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
46
93
96
11
73
91
20
82
45
83
85
19
36
24
3
66
15
93
85
70