From 635ad7ffb29d066ddf369cbfabf7a572df16e917 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Sun, 31 Jan 2021 21:36:01 +0100 Subject: [PATCH] Added utility to generate random strings --- esputils.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/esputils.h b/esputils.h index 0517768..f7a29c4 100644 --- a/esputils.h +++ b/esputils.h @@ -5,8 +5,10 @@ #include #include #include +#include #include #include +#include /* Avoid "unused parameter" warnings @@ -227,5 +229,18 @@ bool is_in(First &&first, T && ... t) return ((first == t) || ...); } +std::string randomString(std::size_t length) +{ + static constexpr auto chars = + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; + thread_local auto rng = std::default_random_engine(); + auto dist = std::uniform_int_distribution{{}, std::strlen(chars) - 1}; + auto result = std::string(length, '\0'); + std::generate_n(std::begin(result), length, [&]() { return chars[dist(rng)]; }); + return result; +} + } // namespace } // namespace espcpputils