From 460f9308a055507020c3182e51bb5c6b8f37ed76 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 4 Apr 2020 18:01:35 +0200 Subject: [PATCH] Fixed issue with Valgrind. --- src/Random.hpp | 30 ++++++++++++------------------ tests/RandomTests.cpp | 2 +- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/Random.hpp b/src/Random.hpp index ca3c706..ac40675 100644 --- a/src/Random.hpp +++ b/src/Random.hpp @@ -9,7 +9,7 @@ namespace Arbutils { template class BaseRandom { private: - uint32_t _seed; + uint_fast32_t _seed; RandomT _rng; std::uniform_real_distribution _distribution; @@ -20,26 +20,22 @@ namespace Arbutils { .count()), _rng(_seed), _distribution(0.0, 1.0) {} - explicit inline constexpr BaseRandom(uint64_t seed) noexcept + explicit inline constexpr BaseRandom(uint_fast32_t seed) noexcept : _seed(seed), _rng(seed), _distribution(0.0, 1.0){}; /// Gets a random float between 0.0 and 1.0. - [[nodiscard]] inline constexpr float GetFloat() noexcept { - return static_cast(GetDouble()); - } + [[nodiscard]] inline constexpr float GetFloat() noexcept { return static_cast(GetDouble()); } /// Gets a random double between 0.0 and 1.0. - [[nodiscard]] inline constexpr double GetDouble() noexcept { - return (double) _distribution(_rng); - } + [[nodiscard]] inline constexpr double GetDouble() noexcept { return _distribution(_rng); } /// Gets a random 32 bit integer - [[nodiscard]] inline constexpr int32_t Get() noexcept { return (int32_t)_rng(); } + [[nodiscard]] inline constexpr int32_t Get() noexcept { return static_cast(_rng()); } /// Gets a random 32 bit integer between 0, and given max parameter. /// \param max The exclusive max value the random value should be. [[nodiscard]] inline int32_t Get(int32_t max) noexcept { - std::uniform_int_distribution distribution(0, max - 1); + std::uniform_int_distribution distribution(0, max - 1); return distribution(_rng); } @@ -47,19 +43,17 @@ namespace Arbutils { /// \param min The inclusive min value the random value should be. /// \param max The exclusive max value the random value should be. [[nodiscard]] inline int32_t Get(int32_t min, int32_t max) noexcept { - std::uniform_int_distribution distribution(min, max - 1); + std::uniform_int_distribution distribution(min, max - 1); return distribution(_rng); } /// Gets a random 32 bit unsigned integer between 0 and max unsigned int. - [[nodiscard]] inline constexpr uint32_t GetUnsigned() noexcept { - return _rng(); - } + [[nodiscard]] inline constexpr uint32_t GetUnsigned() noexcept { return _rng(); } /// Gets a random 32 bit unsigned integer between 0, and given max parameter. /// \param max The exclusive max value the random value should be. [[nodiscard]] inline uint32_t GetUnsigned(uint32_t max) noexcept { - std::uniform_int_distribution distribution(0, max - 1); + std::uniform_int_distribution distribution(0, max - 1); return distribution(_rng); } @@ -67,17 +61,17 @@ namespace Arbutils { /// \param min The inclusive min value the random value should be. /// \param max The exclusive max value the random value should be. [[nodiscard]] inline uint32_t GetUnsigned(uint32_t min, uint32_t max) noexcept { - std::uniform_int_distribution distribution(min, max - 1); + std::uniform_int_distribution distribution(min, max - 1); return distribution(_rng); } - [[nodiscard]] inline constexpr uint32_t GetSeed() const noexcept { return _seed; } + [[nodiscard]] inline constexpr uint_fast32_t GetSeed() const noexcept { return _seed; } }; class Random : public BaseRandom { public: constexpr Random() : BaseRandom() {} - constexpr Random(int32_t seed) : BaseRandom(seed) {} + constexpr Random(uint_fast32_t seed) : BaseRandom(seed) {} }; } #endif // ARBUTILS_RANDOM_HPP diff --git a/tests/RandomTests.cpp b/tests/RandomTests.cpp index 2bce9f9..8b3d690 100644 --- a/tests/RandomTests.cpp +++ b/tests/RandomTests.cpp @@ -93,7 +93,7 @@ TEST_CASE("Random distribution (max 0, min 2)", "[Utilities]") { TEST_CASE("Random distribution (max 0, min 3)", "[Utilities]") { auto rand = Arbutils::Random(10); - const int size = 500000; + const size_t size = 100000; int arr[size]; for (size_t i = 0; i < size; i++) { arr[i] = rand.Get(0, 3);