Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <istream>
- #include <random>
- #include <string>
- #include <utility>
- #include <map>
- #include <sstream>
- std::string random_line(std::istream& input)
- {
- static auto gen = std::mt19937{std::random_device{}()};
- auto count = 0u;
- auto selected = std::string{};
- std::string line;
- while (std::getline(input, line)) {
- if (std::uniform_int_distribution{0u,count++}(gen) == 0) {
- selected = std::move(line);
- }
- }
- return selected;
- }
- int main()
- {
- auto const lines = "one\n"
- "two\n"
- "three\n"
- "four\n"
- "five\n"
- "six\n";
- auto counts = std::map<std::string,unsigned int>{};
- for (auto i = 0; i < 120000; ++i) {
- auto is = std::istringstream{lines};
- ++counts[random_line(is)];
- }
- {
- // print the counts
- auto is = std::istringstream{lines};
- std::string line;
- while (std::getline(is, line)) {
- std::cout << line << ": " << counts[line] << '\n';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment