From e57daf02d0eece5179aef3d0e3f6a336096d4bb5 Mon Sep 17 00:00:00 2001 From: otavepto <153766569+otavepto@users.noreply.github.com> Date: Wed, 5 Jun 2024 22:56:51 +0300 Subject: [PATCH] helper function to generate rand num --- helpers/common_helpers.cpp | 11 +++++++++++ helpers/common_helpers/common_helpers.hpp | 3 +++ 2 files changed, 14 insertions(+) diff --git a/helpers/common_helpers.cpp b/helpers/common_helpers.cpp index 3f6c5a41..98c59e5a 100644 --- a/helpers/common_helpers.cpp +++ b/helpers/common_helpers.cpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace common_helpers { @@ -396,3 +397,13 @@ bool common_helpers::dir_exist(const std::wstring &dirpath) if (dirpath.empty()) return false; 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 distrib(0, max); + + return distrib(gen); +} diff --git a/helpers/common_helpers/common_helpers.hpp b/helpers/common_helpers/common_helpers.hpp index 4ee54864..05967a7a 100644 --- a/helpers/common_helpers/common_helpers.hpp +++ b/helpers/common_helpers/common_helpers.hpp @@ -98,4 +98,7 @@ bool dir_exist(const std::filesystem::path &dirpath); bool dir_exist(const std::string &dirpath); bool dir_exist(const std::wstring &dirpath); +// between 0 and max, 0 and max are included +size_t rand_number(size_t max); + }