Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <random>
- const std::string upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- const std::string lowerCase = "abcdefghijklmnopqrstuvwxyz";
- const std::string digits = "0123456789";
- const std::string special = "!\"#$%&'*+,./:;=?@\\^`|~";
- std::random_device dev;
- std::mt19937 rng(dev());
- std::uniform_int_distribution<std::mt19937::result_type> dist4(1, 4);
- std::uniform_int_distribution<std::mt19937::result_type> distUC(0, upperCase.length()-1);
- std::uniform_int_distribution<std::mt19937::result_type> distLC(0, lowerCase.length()-1);
- std::uniform_int_distribution<std::mt19937::result_type> distDig(0, digits.length()-1);
- std::uniform_int_distribution<std::mt19937::result_type> distSpec(0, special.length()-1);
- std::string generate(int len){
- std::string result{};
- int group{};
- int index{};
- for(size_t i = 0; i < len; ++i){
- group = dist4(rng);
- switch(group){
- case 1:
- index = distUC(rng);
- result += upperCase[index];
- break;
- case 2:
- index = distLC(rng);
- result += lowerCase[index];
- break;
- case 3:
- index = distDig(rng);
- result += digits[index];
- break;
- case 4:
- index = distSpec(rng);
- result += special[index];
- break;
- }
- }
- return result;
- }
- int main()
- {
- int ln = 10;
- for(int i = 0; i < 5; ++i){
- std::cout << generate(ln) << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement