penguins  1.0.0
BetterRng Struct Reference
Inheritance diagram for BetterRng:
[legend]

Detailed Description

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
 
- Data Fields inherited from Rng
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...
 

Field Documentation

◆ rng_engine

std::default_random_engine BetterRng::rng_engine

Definition at line 22 of file better_random.hh.

Referenced by BetterRng().

Constructor & Destructor Documentation

◆ BetterRng()

BetterRng::BetterRng ( )

Seeds the rng_engine using std::random_device.

Definition at line 5 of file better_random.cc.

Member Function Documentation

◆ random_range_impl()

int BetterRng::random_range_impl ( Rng rng,
int  min,
int  max 
)
staticprivate

See Rng::random_range.

Definition at line 14 of file better_random.cc.

Referenced by BetterRng().


The documentation for this struct was generated from the following files: