fork(5) download
  1. #include <cstdlib>
  2. #include <ctime>
  3. #include <random>
  4. #include <iterator>
  5. #include <iostream>
  6.  
  7. int random_int( std::mt19937& rng, int low, int high )
  8. { return std::uniform_int_distribution<int>( low, high )(rng) ; }
  9.  
  10. int main()
  11. {
  12. // initialize random number generator with warm-up sequence
  13. // somewhat elaborate because: 'nearly perfect pseudo random numbers'
  14. std::srand( std::time(nullptr) ) ;
  15. std::uint32_t r[ std::mt19937::state_size ] ;
  16. for( std::uint32_t& n : r ) n = std::rand() ;
  17. std::seed_seq sseq( std::begin(r), std::end(r) ) ;
  18. std::mt19937 gen(sseq) ;
  19.  
  20. // generate and print out 20 random numbers in [0,9]
  21. for( int i = 0 ; i < 20 ; ++i ) std::cout << random_int( gen, 0, 9 ) << ' ' ;
  22. std::cout << '\n' ;
  23.  
  24. // generate and print out 10 random numbers in [55,72]
  25. for( int i = 0 ; i < 10 ; ++i ) std::cout << random_int( gen, 55, 72 ) << ' ' ;
  26. std::cout << '\n' ;
  27. }
  28.  
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
2 4 0 0 9 8 1 6 3 8 2 0 0 0 9 5 6 5 6 9 
58 56 72 69 59 62 62 69 71 64