diff --git a/lib/src/Utilities.cc b/lib/src/Utilities.cc index 6d9fc46d..4020c34a 100644 --- a/lib/src/Utilities.cc +++ b/lib/src/Utilities.cc @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -162,28 +163,16 @@ bool isBase64(std::string_view str) std::string genRandomString(int length) { - static const char char_space[] = + static const std::string_view char_space = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; - static std::once_flag once; - static const size_t len = strlen(char_space); - static const int randMax = RAND_MAX - (RAND_MAX % len); - std::call_once(once, []() { - std::srand(static_cast(time(nullptr))); - }); + std::uniform_int_distribution dist(0, char_space.size() - 1); + thread_local std::mt19937 rng(std::random_device{}()); - int i; std::string str; str.resize(length); - - for (i = 0; i < length; ++i) + for (char &ch : str) { - int x = std::rand(); - while (x >= randMax) - { - x = std::rand(); - } - x = (x % len); - str[i] = char_space[x]; + ch = char_space[dist(rng)]; } return str;