2020-02-26 11:57:18 +00:00
|
|
|
#ifndef ARBUTILS_RANDOM_HPP
|
|
|
|
#define ARBUTILS_RANDOM_HPP
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <random>
|
2020-02-26 12:27:56 +00:00
|
|
|
#include <chrono>
|
2020-02-26 11:57:18 +00:00
|
|
|
|
|
|
|
namespace Arbutils {
|
|
|
|
|
|
|
|
template <class RandomT> class BaseRandom {
|
|
|
|
private:
|
|
|
|
RandomT _rng;
|
|
|
|
|
|
|
|
public:
|
2020-02-26 12:27:56 +00:00
|
|
|
inline constexpr BaseRandom()
|
|
|
|
: _rng(RandomT(
|
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
|
|
|
|
.count())) {}
|
|
|
|
|
|
|
|
explicit inline constexpr BaseRandom(int32_t seed) : _rng(seed){};
|
2020-02-26 11:57:18 +00:00
|
|
|
|
|
|
|
/// Gets a random float between 0.0 and 1.0.
|
2020-02-26 12:27:56 +00:00
|
|
|
[[nodiscard]] inline constexpr float GetFloat() {
|
2020-02-26 11:57:18 +00:00
|
|
|
return static_cast<float>(_rng()) / (static_cast<float>(std::mt19937::max() - std::mt19937::min()) - 0.5f);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets a random double between 0.0 and 1.0.
|
2020-02-26 12:27:56 +00:00
|
|
|
[[nodiscard]] inline constexpr double GetDouble() {
|
2020-02-26 11:57:18 +00:00
|
|
|
return static_cast<double>(_rng()) /
|
|
|
|
((static_cast<double>(std::mt19937::max()) - std::mt19937::min()) - 0.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets a random 32 bit integer between 0 and max int.
|
2020-02-26 12:27:56 +00:00
|
|
|
[[nodiscard]] inline constexpr int32_t Get() {
|
2020-02-26 11:57:18 +00:00
|
|
|
return static_cast<int32_t>(GetDouble() * static_cast<float>(std::numeric_limits<int32_t>::max()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets a random 32 bit integer between 0, and given max parameter.
|
|
|
|
/// \param max The exclusive max value the random value should be.
|
2020-02-26 12:27:56 +00:00
|
|
|
[[nodiscard]] inline constexpr int32_t Get(int32_t max) {
|
2020-02-26 11:57:18 +00:00
|
|
|
return static_cast<int32_t>(GetDouble() * static_cast<float>(max));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
2020-02-26 12:27:56 +00:00
|
|
|
[[nodiscard]] inline constexpr int32_t Get(int32_t min, int32_t max) {
|
2020-02-26 11:57:18 +00:00
|
|
|
return static_cast<int32_t>(GetDouble() * static_cast<float>(max - min) + min);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets a random 32 bit unsigned integer between 0 and max unsigned int.
|
2020-02-26 12:27:56 +00:00
|
|
|
[[nodiscard]] inline constexpr uint32_t GetUnsigned() {
|
2020-02-26 11:57:18 +00:00
|
|
|
return static_cast<int32_t>(GetDouble() * static_cast<float>(std::numeric_limits<uint32_t>::max()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets a random 32 bit unsigned integer between 0, and given max parameter.
|
|
|
|
/// \param max The exclusive max value the random value should be.
|
2020-02-26 12:27:56 +00:00
|
|
|
[[nodiscard]] inline constexpr uint32_t GetUnsigned(uint32_t max) {
|
2020-02-26 11:57:18 +00:00
|
|
|
return static_cast<int32_t>(GetDouble() * static_cast<float>(max));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
2020-02-26 12:27:56 +00:00
|
|
|
[[nodiscard]] inline constexpr uint32_t GetUnsigned(uint32_t min, uint32_t max) {
|
2020-02-26 11:57:18 +00:00
|
|
|
return static_cast<int32_t>(GetDouble() * static_cast<float>(max - min) + min);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Random : public BaseRandom<std::mt19937> {
|
|
|
|
public:
|
|
|
|
Random() : BaseRandom() {}
|
|
|
|
Random(int32_t seed) : BaseRandom(seed) {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif // ARBUTILS_RANDOM_HPP
|