I have written a simple function that returns a random string.
std::string cache::generateCacheName()
{
static const char pool[] = "0123456789abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string r;
std::random_device rd;
std::mt19937 eng(rd());
std::uniform_int_distribution<> distr(0, sizeof(pool) - 1);
for (int i = 0; i < 48; i++)
r += pool[distr(eng)];
return r;
}
However, sometimes it returns a string randomly shorter than 48 characters.
I already added the - 1 behind the sizeof(pool) trying to avoid adding the null-terminator from pool, but that didn't change anything.
Where did I go wrong?
Aucun commentaire:
Enregistrer un commentaire