diff --git a/include/rng.h b/include/rng.h index 7fe4018..06c6eb7 100644 --- a/include/rng.h +++ b/include/rng.h @@ -78,7 +78,7 @@ struct Random virtual bool next_bool(); -#define NDEF(t, M) inline t next_ ## t() { return next_ ## t(M); } +#define NDEF(t, M) inline virtual t next_ ## t() { return next_ ## t(M); } #define NDEFF(n) NDEF(i ## n, INT ## n ## _MAX) NDEF(u ## n, UINT ## n ## _MAX) NDEFF(8) NDEFF(16) diff --git a/include/rng/crand.h b/include/rng/crand.h index 33f0331..8ad60d0 100644 --- a/include/rng/crand.h +++ b/include/rng/crand.h @@ -13,6 +13,9 @@ namespace rng crand(u64 seed); inline ~crand(){} + + inline i64 next_i64() override { return _sample_raw(); } + u64 next_u64() override; protected: f64 _sample() override; private: diff --git a/src/rng/crand.cpp b/src/rng/crand.cpp index 178ad9d..6c8e382 100644 --- a/src/rng/crand.cpp +++ b/src/rng/crand.cpp @@ -9,7 +9,7 @@ #include "crand.h" namespace rng -{ +{ void crand::_deleter::delete_object(_opaque** state) { _jr_free(reinterpret_cast(*state)); *state = nullptr; } crand::crand(_opaque* raw) :_state(mem::aligned_ptr<_opaque, _deleter>(raw)){} @@ -21,6 +21,10 @@ namespace rng //TODO: properly implemet this f64 crand::_sample() { return (f64)std::bit_cast(_sample_raw()) / (f64)UINT64_MAX; } + // Overrides // + u64 crand::next_u64() { return std::bit_cast(_sample_raw()); } + //TODO: Implementing next_i64(i64 max) should be trivial. the man page for drand48 shows its output range, just map that range to `0..=max` + //TODO: make the default range for all non-bounded `next_i/u*` the same range as drand48's. If that's too big for the integer type, scale it down. } void rng_test()