Advertisement
awsmpshk

Randomize string with any length

Oct 6th, 2021
559
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. std::string random_string(size_t length) {
  2.     auto randchar = []() -> char {
  3.         const char charset[] =
  4.             "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  5.             "abcdefghijklmnopqrstuvwxyz";
  6.         const size_t max_index = (sizeof(charset) - 1);
  7.         return charset[rand() % max_index];
  8.     };
  9.     std::string str(length, 0);
  10.     std::generate_n(str.begin(), length, randchar);
  11.  
  12.     return str;
  13. }
Advertisement
RAW Paste Data Copied
Advertisement