helper function to generate rand num

This commit is contained in:
otavepto 2024-06-05 22:56:51 +03:00
parent 663f1a9350
commit e57daf02d0
2 changed files with 14 additions and 0 deletions

View File

@ -3,6 +3,7 @@
#include <cwchar> #include <cwchar>
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <random>
namespace common_helpers { namespace common_helpers {
@ -396,3 +397,13 @@ bool common_helpers::dir_exist(const std::wstring &dirpath)
if (dirpath.empty()) return false; if (dirpath.empty()) return false;
return dir_exist(std::filesystem::path(dirpath)); return dir_exist(std::filesystem::path(dirpath));
} }
size_t common_helpers::rand_number(size_t max)
{
// https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution
std::random_device rd{}; // a seed source for the random number engine
std::mt19937 gen(rd()); // mersenne_twister_engine seeded with rd()
std::uniform_int_distribution<size_t> distrib(0, max);
return distrib(gen);
}

View File

@ -98,4 +98,7 @@ bool dir_exist(const std::filesystem::path &dirpath);
bool dir_exist(const std::string &dirpath); bool dir_exist(const std::string &dirpath);
bool dir_exist(const std::wstring &dirpath); bool dir_exist(const std::wstring &dirpath);
// between 0 and max, 0 and max are included
size_t rand_number(size_t max);
} }