Why can"t I call Random:: functions from an instance of derrived class crand???

Fortune for cpprng's current commit: Curse − 凶
lib
Avril 3 years ago
parent 5f57115683
commit 9b2b2468cc
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -69,9 +69,10 @@ struct Random
Random* const rng; Random* const rng;
}; };
public:
constexpr inline Random(){} inline Random(){}
constexpr inline virtual ~Random(){} inline virtual ~Random(){}
inline f64 next_f64() { return sample(); } inline f64 next_f64() { return sample(); }
inline f32 next_f32() { return (f32)sample(); } inline f32 next_f32() { return (f32)sample(); }

@ -2,7 +2,7 @@
#include "../common.h" #include "../common.h"
#include "../rng.h" #include <rng.h>
#include "../mem.h" #include "../mem.h"
namespace rng namespace rng
@ -16,7 +16,7 @@ namespace rng
crand(); crand();
crand(u64 seed); crand(u64 seed);
inline ~crand(){} inline ~crand() override{}
inline i64 next_i64() override { return _sample_int(); } inline i64 next_i64() override { return _sample_int(); }
u64 next_u64() override; u64 next_u64() override;

@ -3,6 +3,8 @@
#include <cstdio> #include <cstdio>
using namespace rng;
void r(Random& r) void r(Random& r)
{ {
u8 bytes[32]; u8 bytes[32];
@ -13,8 +15,26 @@ void r(Random& r)
std::array<char, 10> nn = r.next<std::array<char, 10>>(); std::array<char, 10> nn = r.next<std::array<char, 10>>();
} }
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<u8, sizeof(u64)> 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() int main()
{ {
rng_test(); rng_test();

@ -30,7 +30,7 @@ namespace rng
{ {
void crand::_deleter::delete_object(_opaque** state) { _jr_free(reinterpret_cast<jr_state*>(*state)); *state = nullptr; } void crand::_deleter::delete_object(_opaque** state) { _jr_free(reinterpret_cast<jr_state*>(*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(u64 seed) : crand(reinterpret_cast<_opaque*>(_jr_new(seed))){}
crand::crand() : crand(0xabad1dea){} crand::crand() : crand(0xabad1dea){}
@ -65,28 +65,8 @@ namespace rng
} }
void crand::next_v64(u64* p, usize n) 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<u8, sizeof(u64)> 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));
}

Loading…
Cancel
Save