fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <random>
  5.  
  6. using strvec = std::vector<std::string>;
  7.  
  8. const strvec article = { "the", "a", "one", "some", "any" };
  9. const strvec noun = { "boy", "girl", "dog", "town", "car" };
  10. const strvec verb = { "drove", "jumped", "ran", "walked", "skipped" };
  11. const strvec preposition= { "to", "from", "over", "on" };
  12.  
  13. template <typename T>
  14. T random(T min, T max)
  15. {
  16. static std::mt19937 rng;
  17. std::uniform_int_distribution<T> dist(min,max);
  18. return dist(rng);
  19. }
  20.  
  21. const std::string & get_random(const strvec& v)
  22. {
  23. return v[random<size_t>(0,v.size()-1)];
  24. }
  25.  
  26. int main()
  27. {
  28. for(size_t i=0; i< 10; i++)
  29. {
  30. std::cout << get_random(article) << " "
  31. << get_random(noun) << " "
  32. << get_random(verb) << " "
  33. << get_random(preposition) << " "
  34. << get_random(noun) << "."
  35. << std::endl;
  36. }
  37. return 0;
  38. }
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
the car skipped to car.
a town jumped on car.
one boy jumped over boy.
any car skipped on car.
any car skipped over boy.
a car walked from boy.
any boy ran to boy.
one car skipped on town.
any girl drove on town.
a town walked on girl.