Advertisement
akela43

random_to_vector

Dec 1st, 2021
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <random>
  2. #include <iterator>
  3. #include <iostream>
  4. #include <vector>
  5. #include <queue>
  6.  
  7. // A simple toolkit random
  8. std::default_random_engine & global_urng( )
  9. {
  10. static std::default_random_engine u{};
  11. return u;
  12. }
  13. void randomize( )
  14. {
  15.     static std::random_device rd{};
  16.     global_urng().seed( rd() );
  17. }
  18. int pick( int from, int thru )
  19. {
  20. static std::uniform_int_distribution<> d{};
  21.  using parm_t = decltype(d)::param_type;
  22.  return d( global_urng(), parm_t{from, thru} );
  23. }
  24. double pick( double from, double upto )
  25. {
  26.  static std::uniform_real_distribution<> d{};
  27.  using parm_t = decltype(d)::param_type;
  28.  return d( global_urng(), parm_t{from, upto} );
  29.  }
  30. // print container
  31. template <typename T> void print(T & v, const std::string &s = "")
  32. {
  33.     if (s.size() >0)    std::cout << s << ": \n";
  34.     for (auto && x:v)   std::cout << x << " ";
  35.     puts("\n");
  36.  
  37. }
  38.  
  39.  
  40. using namespace std;
  41. int main()
  42. {
  43.     vector<int> vec(100);
  44.    
  45.     randomize();
  46.     for (auto &x :vec) x = pick(1, 10);
  47.    
  48.     deque<int> deq{begin(vec),end(vec)};
  49.    
  50.     vector<int> vec_odd;
  51.     vector<int> vec_even;
  52.     for (const auto & x : vec) 
  53.         x % 2 ? vec_even.push_back(x) : vec_odd.push_back(x);
  54.  
  55.     print(vec, "vec");
  56.     print(deq, "deq");
  57.     print(vec_odd, "четные");
  58.     print(vec_even, "нечетные");
  59.     return 0;
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement