fork(1) download
  1. #include <vector>
  2. #include <chrono>
  3. #include <random>
  4. #include <iostream>
  5.  
  6. auto main()->int {
  7. std::vector<std::string> names; // storage for the names
  8. names.reserve(5); // always reserve ahead, for top performance
  9. names.emplace_back("John"); // emplace instead of push to avoid copies
  10. names.emplace_back("Jeff");
  11. names.emplace_back("Emma");
  12. names.emplace_back("Steve");
  13. names.emplace_back("Julie");
  14.  
  15. std::mt19937_64 engine; // make sure we use a high quality RNG engine
  16. auto seed((engine, std::chrono::system_clock::now().time_since_epoch().count())); // RNG seed
  17. std::uniform_int_distribution<unsigned> dist(0, names.size() - 1); // distribute linearly
  18. auto number(dist(engine)); // pick a number corresponding to a name
  19. std::string name(names.at(number)); // look up the name by number
  20. std::cout << "Seed: " << seed << ", name: " << name << std::endl; // output the name & seed
  21. return EXIT_SUCCESS; // don't forget to exit politely
  22. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
Seed: 1403028563522116498, name: Steve