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