fork(1) download
  1. #include <cstdlib>
  2. #include <ctime>
  3. #include <algorithm>
  4. #include <iterator>
  5. #include <iostream>
  6.  
  7. int main()
  8. {
  9. constexpr int MIN = 1 ;
  10. constexpr int MAX = 50 ;
  11. constexpr std::size_t SZ = MAX - MIN + 1 ;
  12. constexpr std::size_t N = 7 ;
  13.  
  14. std::srand( std::time(nullptr) ) ;
  15.  
  16. // 1. create an array containing numbers 1 to 50
  17. int a[SZ] ;
  18. std::iota( std::begin(a), std::end(a), MIN ) ;
  19.  
  20. // 2. generate a random permutation of the array
  21. std::random_shuffle( std::begin(a), std::end(a) ) ;
  22.  
  23. // 3. pick the first 7 numbers
  24. for( std::size_t i = 0 ; i < N ; ++i ) std::cout << a[i] << ' ' ;
  25. std::cout << '\n' ;
  26. }
  27.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
8 7 4 37 43 15 48