Fixed issue with Valgrind.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-04-04 18:01:35 +02:00
parent 6f60cd7c96
commit 460f9308a0
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 13 additions and 19 deletions

View File

@ -9,7 +9,7 @@ namespace Arbutils {
template <class RandomT> class BaseRandom {
private:
uint32_t _seed;
uint_fast32_t _seed;
RandomT _rng;
std::uniform_real_distribution<double> _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<float>(GetDouble());
}
[[nodiscard]] inline constexpr float GetFloat() noexcept { return static_cast<float>(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<int32_t>(_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<int32_t > distribution(0, max - 1);
std::uniform_int_distribution<int32_t> 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<int32_t > distribution(min, max - 1);
std::uniform_int_distribution<int32_t> 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<uint32_t > distribution(0, max - 1);
std::uniform_int_distribution<uint32_t> 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<uint32_t > distribution(min, max - 1);
std::uniform_int_distribution<uint32_t> 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<std::mt19937> {
public:
constexpr Random() : BaseRandom() {}
constexpr Random(int32_t seed) : BaseRandom(seed) {}
constexpr Random(uint_fast32_t seed) : BaseRandom(seed) {}
};
}
#endif // ARBUTILS_RANDOM_HPP

View File

@ -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);