penguins
1.0.0
|
An implementation of Rng for C++ using the standard functions from <random>
.
Here I abuse the fact that if class B inherits from class A, given an instance of B a pointer of the type A may be created to it and using the members of the base class A through said pointer afterwards is safe and perfectly fine. This works because the fields of descendant classes are laid out in memory after the fields of the base class, so if we have a pointer of the base type to an object that actually extends that base class, we are going to access just the leading chunk of memory.
Strictly speaking implementing this struct like this wasn't required, but inheriting from a C struct is nonetheless a cool hack. On the other hand, now BetterRng can be passed anywhere Rng can.
Definition at line 20 of file better_random.hh.
Public Member Functions | |
BetterRng () | |
Seeds the rng_engine using std::random_device . More... | |
Data Fields | |
std::default_random_engine | rng_engine |
![]() | |
int(* | random_range )(struct Rng *self, int min, int max) |
Generates and returns a value in the range [min; max) (i.e. from min inclusive to max exclusive). More... | |
Static Private Member Functions | |
static int | random_range_impl (Rng *rng, int min, int max) |
See Rng::random_range. More... | |
std::default_random_engine BetterRng::rng_engine |
Definition at line 22 of file better_random.hh.
Referenced by BetterRng().
BetterRng::BetterRng | ( | ) |
Seeds the rng_engine using std::random_device
.
Definition at line 5 of file better_random.cc.
|
staticprivate |