diff --git a/src/functions/MemoryTestFunction.cpp b/src/functions/MemoryTestFunction.cpp index 084cab5..3448295 100644 --- a/src/functions/MemoryTestFunction.cpp +++ b/src/functions/MemoryTestFunction.cpp @@ -71,11 +71,9 @@ RUN { LOGN(MTFUN, INFO) << "Generating random data for testing" << std::endl; auto *buffer = new (std::nothrow) char[bufferSize]; collector.delAfterProgress(buffer); - std::mt19937 rng(std::random_device{}()); - std::uniform_int_distribution dist(0, 255); for (size_t i = 0; i < bufferSize; i++) - buffer[i] = static_cast(dist(rng)); + buffer[i] = static_cast(Helper::Random<1024>::getNumber()); collector.delFileAfterProgress(test); diff --git a/srclib/libhelper/include/libhelper/lib.hpp b/srclib/libhelper/include/libhelper/lib.hpp index 2425b48..b3084b4 100644 --- a/srclib/libhelper/include/libhelper/lib.hpp +++ b/srclib/libhelper/include/libhelper/lib.hpp @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include #include #include @@ -113,6 +115,48 @@ public: void closeAfterProgress(int _fd); }; +template +class Random { + static_assert(max > start, "max is larger than start"); + static_assert(count > 1, "count is larger than 1"); + static_assert(count <= max - start, "count is greater than max-start"); + +public: + static std::set get() { + std::set set; + std::random_device rd; + std::mt19937 gen(rd()); + + if constexpr (d > 0) { + std::uniform_int_distribution<> dist(0, (max - start - 1) / d); + while (set.size() < count) + set.insert(start + dist(gen) * d); + } else { + std::uniform_int_distribution<> dist(start, max - 1); + while (set.size() < count) + set.insert(dist(gen)); + } + + return set; + } + + static int getNumber() { + std::random_device rd; + std::mt19937 gen(rd()); + int ret; + + if constexpr (d > 0) { + std::uniform_int_distribution<> dist(0, (max - start - 1) / d); + ret = start + dist(gen) * d; + } else { + std::uniform_int_distribution<> dist(start, max - 1); // max exclusive + ret = dist(gen); + } + + return ret; + } +}; + namespace LoggingProperties { extern std::string_view FILE, NAME; extern bool PRINT, DISABLE;