72 lines
2.8 KiB
C++
72 lines
2.8 KiB
C++
|
#ifndef ARBUTILS_RANDOM_HPP
|
||
|
#define ARBUTILS_RANDOM_HPP
|
||
|
|
||
|
#include <cstdint>
|
||
|
#include <random>
|
||
|
|
||
|
namespace Arbutils {
|
||
|
|
||
|
template <class RandomT> class BaseRandom {
|
||
|
private:
|
||
|
RandomT _rng;
|
||
|
|
||
|
public:
|
||
|
BaseRandom();
|
||
|
explicit BaseRandom(int32_t seed) : _rng(seed){};
|
||
|
|
||
|
/// Gets a random float between 0.0 and 1.0.
|
||
|
[[nodiscard]] inline float GetFloat() {
|
||
|
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.
|
||
|
[[nodiscard]] inline double GetDouble() {
|
||
|
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.
|
||
|
[[nodiscard]] inline int32_t Get() {
|
||
|
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.
|
||
|
[[nodiscard]] inline int32_t Get(int32_t max) {
|
||
|
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.
|
||
|
[[nodiscard]] inline int32_t Get(int32_t min, int32_t max) {
|
||
|
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.
|
||
|
[[nodiscard]] inline uint32_t GetUnsigned() {
|
||
|
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.
|
||
|
[[nodiscard]] inline uint32_t GetUnsigned(uint32_t max) {
|
||
|
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.
|
||
|
[[nodiscard]] inline uint32_t GetUnsigned(uint32_t min, uint32_t max) {
|
||
|
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
|