Fixed issue with Valgrind.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
6f60cd7c96
commit
460f9308a0
|
@ -9,7 +9,7 @@ namespace Arbutils {
|
||||||
|
|
||||||
template <class RandomT> class BaseRandom {
|
template <class RandomT> class BaseRandom {
|
||||||
private:
|
private:
|
||||||
uint32_t _seed;
|
uint_fast32_t _seed;
|
||||||
RandomT _rng;
|
RandomT _rng;
|
||||||
std::uniform_real_distribution<double> _distribution;
|
std::uniform_real_distribution<double> _distribution;
|
||||||
|
|
||||||
|
@ -20,26 +20,22 @@ namespace Arbutils {
|
||||||
.count()),
|
.count()),
|
||||||
_rng(_seed), _distribution(0.0, 1.0) {}
|
_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){};
|
: _seed(seed), _rng(seed), _distribution(0.0, 1.0){};
|
||||||
|
|
||||||
/// Gets a random float between 0.0 and 1.0.
|
/// Gets a random float between 0.0 and 1.0.
|
||||||
[[nodiscard]] inline constexpr float GetFloat() noexcept {
|
[[nodiscard]] inline constexpr float GetFloat() noexcept { return static_cast<float>(GetDouble()); }
|
||||||
return static_cast<float>(GetDouble());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gets a random double between 0.0 and 1.0.
|
/// Gets a random double between 0.0 and 1.0.
|
||||||
[[nodiscard]] inline constexpr double GetDouble() noexcept {
|
[[nodiscard]] inline constexpr double GetDouble() noexcept { return _distribution(_rng); }
|
||||||
return (double) _distribution(_rng);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gets a random 32 bit integer
|
/// 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.
|
/// Gets a random 32 bit integer between 0, and given max parameter.
|
||||||
/// \param max The exclusive max value the random value should be.
|
/// \param max The exclusive max value the random value should be.
|
||||||
[[nodiscard]] inline int32_t Get(int32_t max) noexcept {
|
[[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);
|
return distribution(_rng);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,19 +43,17 @@ namespace Arbutils {
|
||||||
/// \param min The inclusive min value the random value should be.
|
/// \param min The inclusive min value the random value should be.
|
||||||
/// \param max The exclusive max 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 {
|
[[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);
|
return distribution(_rng);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets a random 32 bit unsigned integer between 0 and max unsigned int.
|
/// Gets a random 32 bit unsigned integer between 0 and max unsigned int.
|
||||||
[[nodiscard]] inline constexpr uint32_t GetUnsigned() noexcept {
|
[[nodiscard]] inline constexpr uint32_t GetUnsigned() noexcept { return _rng(); }
|
||||||
return _rng();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gets a random 32 bit unsigned integer between 0, and given max parameter.
|
/// Gets a random 32 bit unsigned integer between 0, and given max parameter.
|
||||||
/// \param max The exclusive max value the random value should be.
|
/// \param max The exclusive max value the random value should be.
|
||||||
[[nodiscard]] inline uint32_t GetUnsigned(uint32_t max) noexcept {
|
[[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);
|
return distribution(_rng);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,17 +61,17 @@ namespace Arbutils {
|
||||||
/// \param min The inclusive min value the random value should be.
|
/// \param min The inclusive min value the random value should be.
|
||||||
/// \param max The exclusive max 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 {
|
[[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);
|
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> {
|
class Random : public BaseRandom<std::mt19937> {
|
||||||
public:
|
public:
|
||||||
constexpr Random() : BaseRandom() {}
|
constexpr Random() : BaseRandom() {}
|
||||||
constexpr Random(int32_t seed) : BaseRandom(seed) {}
|
constexpr Random(uint_fast32_t seed) : BaseRandom(seed) {}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif // ARBUTILS_RANDOM_HPP
|
#endif // ARBUTILS_RANDOM_HPP
|
||||||
|
|
|
@ -93,7 +93,7 @@ TEST_CASE("Random distribution (max 0, min 2)", "[Utilities]") {
|
||||||
|
|
||||||
TEST_CASE("Random distribution (max 0, min 3)", "[Utilities]") {
|
TEST_CASE("Random distribution (max 0, min 3)", "[Utilities]") {
|
||||||
auto rand = Arbutils::Random(10);
|
auto rand = Arbutils::Random(10);
|
||||||
const int size = 500000;
|
const size_t size = 100000;
|
||||||
int arr[size];
|
int arr[size];
|
||||||
for (size_t i = 0; i < size; i++) {
|
for (size_t i = 0; i < size; i++) {
|
||||||
arr[i] = rand.Get(0, 3);
|
arr[i] = rand.Get(0, 3);
|
||||||
|
|
Loading…
Reference in New Issue