Rework of Random class. Now ensures better randomness, is more performant, and has function for retrieving seed.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2020-04-04 17:48:05 +02:00
parent 4f563e6e67
commit 6f60cd7c96
2 changed files with 45 additions and 38 deletions

View File

@@ -1,76 +1,83 @@
#ifndef ARBUTILS_RANDOM_HPP
#define ARBUTILS_RANDOM_HPP
#include <chrono>
#include <cstdint>
#include <random>
#include <chrono>
namespace Arbutils {
template <class RandomT> class BaseRandom {
private:
uint32_t _seed;
RandomT _rng;
std::uniform_real_distribution<double> _distribution;
public:
inline constexpr BaseRandom()
: _rng(RandomT(
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
.count())) {}
inline constexpr BaseRandom() noexcept
: _seed(std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count()),
_rng(_seed), _distribution(0.0, 1.0) {}
explicit inline constexpr BaseRandom(int32_t seed) : _rng(seed){};
explicit inline constexpr BaseRandom(uint64_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() {
return static_cast<float>(_rng()) / (static_cast<float>(std::mt19937::max() - std::mt19937::min()) - 0.5f);
[[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() {
return static_cast<double>(_rng()) /
((static_cast<double>(std::mt19937::max()) - std::mt19937::min()) - 0.5);
[[nodiscard]] inline constexpr double GetDouble() noexcept {
return (double) _distribution(_rng);
}
/// Gets a random 32 bit integer between 0 and max int.
[[nodiscard]] inline constexpr int32_t Get() {
return static_cast<int32_t>(GetDouble() * static_cast<float>(std::numeric_limits<int32_t>::max()));
}
/// Gets a random 32 bit integer
[[nodiscard]] inline constexpr int32_t Get() noexcept { return (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 constexpr int32_t Get(int32_t max) {
return static_cast<int32_t>(GetDouble() * static_cast<float>(max));
[[nodiscard]] inline int32_t Get(int32_t max) noexcept {
std::uniform_int_distribution<int32_t > distribution(0, max - 1);
return distribution(_rng);
}
/// 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 constexpr int32_t Get(int32_t min, int32_t max) {
return static_cast<int32_t>(GetDouble() * static_cast<float>(max - min) + min);
[[nodiscard]] inline int32_t Get(int32_t min, int32_t max) noexcept {
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() {
return static_cast<int32_t>(GetDouble() * static_cast<float>(std::numeric_limits<uint32_t>::max()));
[[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 constexpr uint32_t GetUnsigned(uint32_t max) {
return static_cast<int32_t>(GetDouble() * static_cast<float>(max));
[[nodiscard]] inline uint32_t GetUnsigned(uint32_t max) noexcept {
std::uniform_int_distribution<uint32_t > distribution(0, max - 1);
return distribution(_rng);
}
/// 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 constexpr uint32_t GetUnsigned(uint32_t min, uint32_t max) {
return static_cast<int32_t>(GetDouble() * static_cast<float>(max - min) + min);
[[nodiscard]] inline uint32_t GetUnsigned(uint32_t min, uint32_t max) noexcept {
std::uniform_int_distribution<uint32_t > distribution(min, max - 1);
return distribution(_rng);
}
[[nodiscard]] inline constexpr uint32_t GetSeed() const noexcept { return _seed; }
};
class Random : public BaseRandom<std::mt19937> {
public:
Random() : BaseRandom() {}
Random(int32_t seed) : BaseRandom(seed) {}
constexpr Random() : BaseRandom() {}
constexpr Random(int32_t seed) : BaseRandom(seed) {}
};
}
#endif // ARBUTILS_RANDOM_HPP