Assert that max is higher than min on random.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-04-19 18:45:59 +02:00
parent 63da7ffda0
commit 7d98d5b467
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
1 changed files with 8 additions and 4 deletions

View File

@ -4,6 +4,7 @@
#include <chrono>
#include <cstdint>
#include <random>
#include "Assert.hpp"
namespace Arbutils {
@ -34,7 +35,8 @@ namespace Arbutils {
/// 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 {
[[nodiscard]] inline int32_t Get(int32_t max) {
Assert(max > 0);
std::uniform_int_distribution<int32_t> distribution(0, max - 1);
return distribution(_rng);
}
@ -42,7 +44,8 @@ namespace Arbutils {
/// Gets a random 32 bit integer between given min and max parameters.
/// \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 {
[[nodiscard]] inline int32_t Get(int32_t min, int32_t max) {
Assert(max > min);
std::uniform_int_distribution<int32_t> distribution(min, max - 1);
return distribution(_rng);
}
@ -60,7 +63,8 @@ namespace Arbutils {
/// Gets a random 32 bit unsigned integer between given min and max parameters.
/// \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 {
[[nodiscard]] inline uint32_t GetUnsigned(uint32_t min, uint32_t max) {
Assert(max > min);
std::uniform_int_distribution<uint32_t> distribution(min, max - 1);
return distribution(_rng);
}
@ -71,7 +75,7 @@ namespace Arbutils {
class Random : public BaseRandom<std::mt19937> {
public:
constexpr Random() : BaseRandom() {}
constexpr Random(uint_fast32_t seed) : BaseRandom(seed) {}
explicit constexpr Random(uint_fast32_t seed) : BaseRandom(seed) {}
};
}
#endif // ARBUTILS_RANDOM_HPP