Skip to content

Commit

Permalink
Replace rejection sampling and remove use of rand() (#2180)
Browse files Browse the repository at this point in the history
  • Loading branch information
marty1885 authored Oct 10, 2024
1 parent bf1fc03 commit 3fce70b
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions lib/src/Utilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <sstream>
#include <string>
#include <mutex>
#include <random>
#include <algorithm>
#include <array>
#include <locale>
Expand Down Expand Up @@ -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<unsigned int>(time(nullptr)));
});

int i;
std::uniform_int_distribution<size_t> dist(0, char_space.size() - 1);
thread_local std::mt19937 rng(std::random_device{}());

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;
Expand Down

0 comments on commit 3fce70b

Please sign in to comment.