diff --git a/include/rng.h b/include/rng.h index 691d02a..1df12e8 100644 --- a/include/rng.h +++ b/include/rng.h @@ -69,9 +69,10 @@ struct Random Random* const rng; }; + public: - constexpr inline Random(){} - constexpr inline virtual ~Random(){} + inline Random(){} + inline virtual ~Random(){} inline f64 next_f64() { return sample(); } inline f32 next_f32() { return (f32)sample(); } diff --git a/include/rng/crand.h b/include/rng/crand.h index d52b5ef..c104fd8 100644 --- a/include/rng/crand.h +++ b/include/rng/crand.h @@ -2,7 +2,7 @@ #include "../common.h" -#include "../rng.h" +#include #include "../mem.h" namespace rng @@ -16,7 +16,7 @@ namespace rng crand(); crand(u64 seed); - inline ~crand(){} + inline ~crand() override{} inline i64 next_i64() override { return _sample_int(); } u64 next_u64() override; diff --git a/src/main.cpp b/src/main.cpp index 5713278..af9f001 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,8 @@ #include +using namespace rng; + void r(Random& r) { u8 bytes[32]; @@ -13,8 +15,26 @@ void r(Random& r) std::array nn = r.next>(); } -void rng_test(); +void rng_test() +{ + crand _r(123); + Random& r = _r; + printf("%lu %lu %lu\n", r.next_u64(), r.next_u64(), r.next_u64()); + printf("%d %d %d\n", r.next_i32(), r.next_i32(), r.next_i32()); + printf("%u %u %u\n", r.next_u32(), r.next_u32(), r.next_u32()); + + union { + volatile u64 u; + u8 b[sizeof(u64)]; + std::array a; + } thing = {0}; + r.next_bytes(thing.b, sizeof(u64)); + printf("chaos: %lu, %lu, %lu\n", thing.u, (r.next_bytes(thing.a), thing.u), (r.next_bytes(thing.b), thing.u)); + + // TODO: these aren't implemented yet in the base Random huh... + printf("---\n%u %d %d %u\n", r.next_u32(10, 20), r.next_i32(10, 20), r.next_i32(10), r.next_u32(10)); +} int main() { rng_test(); diff --git a/src/rng/crand.cpp b/src/rng/crand.cpp index feab84d..3cc0251 100644 --- a/src/rng/crand.cpp +++ b/src/rng/crand.cpp @@ -30,7 +30,7 @@ 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)){} + crand::crand(_opaque* raw) : Random(), _state(mem::aligned_ptr<_opaque, _deleter>(raw)){} crand::crand(u64 seed) : crand(reinterpret_cast<_opaque*>(_jr_new(seed))){} crand::crand() : crand(0xabad1dea){} @@ -65,28 +65,8 @@ namespace rng } void crand::next_v64(u64* p, usize n) { - while( n --> 0) *p++ = u64(_sample_int() & INT32_MAX) + (u64(_sample_int() & INT32_MAX) << 32); + while( n --> 0 ) *p++ = u64(_sample_int() & INT32_MAX) + (u64(_sample_int() & INT32_MAX) << 32); } } -void rng_test() -{ - rng::crand _r(123); - Random& r = _r; - printf("%lu %lu %lu\n", r.next_u64(), r.next_u64(), r.next_u64()); - printf("%d %d %d\n", r.next_i32(), r.next_i32(), r.next_i32()); - printf("%u %u %u\n", r.next_u32(), r.next_u32(), r.next_u32()); - - union { - volatile u64 u; - u8 b[sizeof(u64)]; - std::array a; - } thing = {0}; - - r.next_bytes(thing.b, sizeof(u64)); - printf("chaos: %lu, %lu, %lu\n", thing.u, (r.next_bytes(thing.a), thing.u), (r.next_bytes(thing.b), thing.u)); - - // TODO: these aren't implemented yet in the base Random huh... - printf("---\n%u %d %d %u\n", r.next_u32(10, 20), r.next_i32(10, 20), r.next_i32(10), r.next_u32(10)); -}