-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrand.cpp
More file actions
26 lines (23 loc) · 785 Bytes
/
rand.cpp
File metadata and controls
26 lines (23 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "rand.hpp"
#include <string>
#include <cstring>
#include <sstream>
int generate_random_num()
{
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_int_distribution<> dis(std::numeric_limits<int>::min(), std::numeric_limits<int>::max());
return dis(gen);
}
auto gen_rand_str(std::size_t len) -> std::string {
static constexpr auto chars =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"!@#$^&*(){}[]|";
thread_local auto rng = random_generator<>();
auto dist = std::uniform_int_distribution{{}, std::strlen(chars) - 1};
auto result = std::string(len, '\0');
std::generate_n(begin(result), len, [&]() { return chars[dist(rng)]; });
return result;
}