fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <iterator>
  6. using namespace std;
  7.  
  8. template<typename T>
  9. struct range {
  10. using value_type = T;
  11. //TODO: assert min <= max
  12. value_type min, max;
  13. };
  14.  
  15. struct point {
  16. using value_type = int;
  17. value_type x, y;
  18.  
  19. static point random(range<value_type> x_r, range<value_type> y_r) {
  20. //TODO: use <random> instead of filthy rand
  21. return {
  22. (rand()%x_r.max)+x_r.min,
  23. (rand()%y_r.max)+y_r.min
  24. };
  25. }
  26.  
  27. friend ostream &operator<<(ostream &os, point const &p) {
  28. return os << "{x: " << p.x << ", y: " << p.y << "}";
  29. }
  30. };
  31. int main() {
  32. range<point::value_type>
  33. x_range { 0, 100 },
  34. y_range { 0, 100 };
  35.  
  36. vector<point> points;
  37. generate_n(back_inserter(points), 10, [&] {
  38. return point::random(x_range, y_range);
  39. });
  40.  
  41. for_each(begin(points), end(points), [](auto &&p) {
  42. cout << p << endl;
  43. });
  44. return 0;
  45. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
{x: 83, y: 86}
{x: 77, y: 15}
{x: 93, y: 35}
{x: 86, y: 92}
{x: 49, y: 21}
{x: 62, y: 27}
{x: 90, y: 59}
{x: 63, y: 26}
{x: 40, y: 26}
{x: 72, y: 36}