vulp  2.3.0
random_string.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright 2022 Stéphane Caron
3 
4 #pragma once
5 
6 #include <algorithm>
7 #include <random>
8 #include <string>
9 
10 namespace vulp::utils {
11 
21 inline std::string random_string(unsigned length = 16) {
22  std::string alphanum(
23  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
24  length = (length <= 62) ? length : 62; // 62 == alphanum.size()
25  std::random_device device;
26  std::mt19937 generator(device());
27  std::shuffle(alphanum.begin(), alphanum.end(), generator);
28  return alphanum.substr(0, length);
29 }
30 
31 } // namespace vulp::utils
Utility functions.
Definition: __init__.py:1
std::string random_string(unsigned length=16)
Generate a random string.
Definition: random_string.h:21