2019-10-06 11:50:52 +00:00
|
|
|
#include "Random.hpp"
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
// Seed parameterless constructor with current milliseconds since epoch.
|
2019-11-28 11:55:22 +00:00
|
|
|
CreatureLib::Core::Random::Random()
|
|
|
|
: _rng(std::mt19937(duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count())) {}
|
2019-10-06 11:50:52 +00:00
|
|
|
|
|
|
|
float CreatureLib::Core::Random::GetFloat() {
|
|
|
|
return static_cast<float>(_rng()) / (static_cast<float>(std::mt19937::max() - std::mt19937::min()) - 0.5f);
|
|
|
|
}
|
|
|
|
|
|
|
|
double CreatureLib::Core::Random::GetDouble() {
|
|
|
|
return static_cast<double>(_rng()) / ((static_cast<double>(std::mt19937::max()) - std::mt19937::min()) - 0.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t CreatureLib::Core::Random::Get() {
|
2019-11-28 11:55:22 +00:00
|
|
|
return static_cast<int32_t>(GetDouble() * static_cast<float>(std::numeric_limits<int32_t>::max()));
|
2019-10-06 11:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t CreatureLib::Core::Random::Get(int32_t max) {
|
2019-11-28 11:55:22 +00:00
|
|
|
return static_cast<int32_t>(GetDouble() * static_cast<float>(max));
|
2019-10-06 11:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t CreatureLib::Core::Random::Get(int32_t min, int32_t max) {
|
2019-11-28 11:55:22 +00:00
|
|
|
return static_cast<int32_t>(GetDouble() * static_cast<float>(max - min) + min);
|
2019-10-06 11:50:52 +00:00
|
|
|
}
|