From dabfe2269d8dde7c951b4a4eb99c76f49eb0f271 Mon Sep 17 00:00:00 2001 From: Luan Luciano Date: Tue, 3 Oct 2023 21:01:14 -0300 Subject: [PATCH 1/3] Update tools.cpp --- src/utils/tools.cpp | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/utils/tools.cpp b/src/utils/tools.cpp index f6b5d9ff161..fef214c2d01 100644 --- a/src/utils/tools.cpp +++ b/src/utils/tools.cpp @@ -398,23 +398,14 @@ int32_t uniform_random(int32_t minNumber, int32_t maxNumber) { int32_t normal_random(int32_t minNumber, int32_t maxNumber) { static std::normal_distribution normalRand(0.5f, 0.25f); - if (minNumber == maxNumber) { - return minNumber; - } else if (minNumber > maxNumber) { - std::swap(minNumber, maxNumber); - } + float v; + do { + v = normalRand(getRandomGenerator()); + } while (v < 0.0 || v > 1.0); - int32_t increment; + std::tie(minNumber, maxNumber) = std::minmax(minNumber, maxNumber); const int32_t diff = maxNumber - minNumber; - const float v = normalRand(getRandomGenerator()); - if (v < 0.0) { - increment = diff / 2; - } else if (v > 1.0) { - increment = (diff + 1) / 2; - } else { - increment = round(v * diff); - } - return minNumber + increment; + return minNumber + std::lround(v * diff); } bool boolean_random(double probability /* = 0.5*/) { From b6631a3b32b313a3b681863171efc020e5cd3fd2 Mon Sep 17 00:00:00 2001 From: Luan Luciano Date: Mon, 16 Oct 2023 10:40:44 -0300 Subject: [PATCH 2/3] Update tools.cpp --- src/utils/tools.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/utils/tools.cpp b/src/utils/tools.cpp index fef214c2d01..cba82d02855 100644 --- a/src/utils/tools.cpp +++ b/src/utils/tools.cpp @@ -403,9 +403,8 @@ int32_t normal_random(int32_t minNumber, int32_t maxNumber) { v = normalRand(getRandomGenerator()); } while (v < 0.0 || v > 1.0); - std::tie(minNumber, maxNumber) = std::minmax(minNumber, maxNumber); - const int32_t diff = maxNumber - minNumber; - return minNumber + std::lround(v * diff); + auto&& [a, b] = std::minmax(minNumber, maxNumber); + return a + std::lround(v * (b - a)); } bool boolean_random(double probability /* = 0.5*/) { From 16ea9912ea2e482674390fbeff372c24ee403f12 Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Mon, 16 Oct 2023 13:41:51 +0000 Subject: [PATCH 3/3] Code format - (Clang-format) --- src/utils/tools.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/tools.cpp b/src/utils/tools.cpp index cba82d02855..26dbe2d415e 100644 --- a/src/utils/tools.cpp +++ b/src/utils/tools.cpp @@ -403,7 +403,7 @@ int32_t normal_random(int32_t minNumber, int32_t maxNumber) { v = normalRand(getRandomGenerator()); } while (v < 0.0 || v > 1.0); - auto&& [a, b] = std::minmax(minNumber, maxNumber); + auto &&[a, b] = std::minmax(minNumber, maxNumber); return a + std::lround(v * (b - a)); }