srng: impl floating point numbers and make std dist compat

This commit is contained in:
Green Sky 2021-06-21 23:07:48 +02:00
parent 573b276116
commit c9db08cb0a
3 changed files with 34 additions and 1 deletions

View File

@ -1,2 +1,16 @@
#include "./srng.hpp"
namespace MM::Random {
template<>
double SRNG::range(const ScalarRange2<double>& range) {
return zeroToOne() * (range.max() - range.min()) + range.min();
}
template<>
float SRNG::range(const ScalarRange2<float>& range) {
return zeroToOne() * (range.max() - range.min()) + range.min();
}
} // MM::Random

View File

@ -37,7 +37,6 @@ struct SRNG {
// more advanced
// inclusive
// TODO: test for floats
template<typename T>
T range(const ScalarRange2<T>& range) {
return (getNext() % ((range.max() - range.min()) + 1)) + range.min();
@ -51,7 +50,22 @@ struct SRNG {
bool operator()(float prob) {
return roll(prob);
}
// std:: distributions need those
constexpr static uint32_t min(void) {
return 0;
}
constexpr static uint32_t max(void) {
return 0xffffffff;
}
};
template<>
double SRNG::range(const ScalarRange2<double>& range);
template<>
float SRNG::range(const ScalarRange2<float>& range);
} // MM::Random

View File

@ -9,6 +9,11 @@ struct ScalarRange2 {
ScalarRange2(void) = default;
ScalarRange2(const T& both) noexcept {
v_min = both;
v_max = both;
}
ScalarRange2(const T& min, const T& max) noexcept {
if (min <= max) {
v_min = min;