dasmig-core 1.2.0
Shared random engine, distributions and data tiers for the dasmig generator libraries
Loading...
Searching...
No Matches
dasmig::core::rng Class Reference

The core random engine: xoshiro256** (Blackman and Vigna 2018). More...

#include <rng.hpp>

Public Types

using result_type = std::uint64_t
 Type of the values produced.
 

Public Member Functions

constexpr rng (std::uint64_t seed) noexcept
 Construct from a seed.
 
constexpr rng & seed (std::uint64_t seed) noexcept
 Reset the engine to the start of the sequence for a seed.
 
rng & unseed ()
 Reseed from std::random_device.
 
constexpr std::uint64_t seed () const noexcept
 The seed this engine was last seeded with.
 
constexpr result_type operator() () noexcept
 Advance and return the next value.
 
constexpr std::uint64_t child_seed (std::string_view label) const noexcept
 Derive the child seed for a label.
 
constexpr std::uint64_t child_seed (std::uint64_t index) const noexcept
 Derive the child seed for an index.
 
constexpr rng split (std::string_view label) const noexcept
 A child engine for a label, independent of this engine's state.
 
constexpr rng split (std::uint64_t index) const noexcept
 A child engine for an index, independent of this engine's state.
 

Static Public Member Functions

static rng from_entropy ()
 Construct with a seed taken from std::random_device.
 
static std::uint64_t entropy_seed ()
 A 64-bit seed from std::random_device.
 
static constexpr std::uint64_t child_seed_of (std::uint64_t seed, std::uint64_t index) noexcept
 child_seed(index) for a seed, without building an engine.
 
static constexpr result_type min () noexcept
 Smallest value produced.
 
static constexpr result_type max () noexcept
 Largest value produced.
 

Friends

constexpr bool operator== (const rng &, const rng &) noexcept=default
 Engines are equal when their seed and position are equal.
 

Detailed Description

The core random engine: xoshiro256** (Blackman and Vigna 2018).

Satisfies std::uniform_random_bit_generator, but use the core's own distributions with it: the std:: distributions give different results on different standard libraries.

An engine remembers the seed it was built from. split() derives a child engine from that seed and a label, not from the current state, so the streams a generator hands to its parts do not shift when another part draws more or fewer numbers.

auto height = r.split("height"); // same stream for seed 42, always
auto weight = r.split("weight"); // independent of height's draws
The core random engine: xoshiro256** (Blackman and Vigna 2018).
Definition rng.hpp:108
constexpr rng split(std::string_view label) const noexcept
A child engine for a label, independent of this engine's state.
Definition rng.hpp:213

Definition at line 107 of file rng.hpp.

Member Typedef Documentation

◆ result_type

using dasmig::core::rng::result_type = std::uint64_t

Type of the values produced.

Definition at line 110 of file rng.hpp.

Constructor & Destructor Documentation

◆ rng()

constexpr dasmig::core::rng::rng ( std::uint64_t  seed)
inlineexplicitconstexprnoexcept

Construct from a seed.

Parameters
seedSeed; every value, including 0, is a valid seed.

Definition at line 114 of file rng.hpp.

Member Function Documentation

◆ child_seed() [1/2]

constexpr std::uint64_t dasmig::core::rng::child_seed ( std::string_view  label) const
inlineconstexprnoexcept

Derive the child seed for a label.

splitmix64_mix(seed ^ splitmix64_mix(fnv1a64(label))).

Parameters
labelUTF-8 label naming the child stream.
Returns
The child seed.

Definition at line 177 of file rng.hpp.

◆ child_seed() [2/2]

constexpr std::uint64_t dasmig::core::rng::child_seed ( std::uint64_t  index) const
inlineconstexprnoexcept

Derive the child seed for an index.

splitmix64_mix(seed ^ splitmix64_mix(index ^ 0x6a09e667f3bcc909)); the constant keeps index streams apart from label streams.

Parameters
indexIndex of the child stream (for example a family member).
Returns
The child seed.

Definition at line 190 of file rng.hpp.

◆ child_seed_of()

static constexpr std::uint64_t dasmig::core::rng::child_seed_of ( std::uint64_t  seed,
std::uint64_t  index 
)
inlinestaticconstexprnoexcept

child_seed(index) for a seed, without building an engine.

Building an engine expands the seed into 256 bits of state; callers that only need the derived seed (item_seed()) skip that.

Parameters
seedParent seed.
indexIndex of the child stream.
Returns
The same value as rng{seed}.child_seed(index).

Definition at line 204 of file rng.hpp.

◆ entropy_seed()

static std::uint64_t dasmig::core::rng::entropy_seed ( )
inlinestatic

A 64-bit seed from std::random_device.

Returns
An unpredictable seed.

Definition at line 126 of file rng.hpp.

◆ from_entropy()

static rng dasmig::core::rng::from_entropy ( )
inlinestatic

Construct with a seed taken from std::random_device.

For the instance engines of non-seeded calls only; anything that must replay takes a seed explicitly.

Returns
An engine with an unpredictable seed.

Definition at line 122 of file rng.hpp.

◆ max()

static constexpr result_type dasmig::core::rng::max ( )
inlinestaticconstexprnoexcept

Largest value produced.

Returns
2^64 - 1.

Definition at line 232 of file rng.hpp.

◆ min()

static constexpr result_type dasmig::core::rng::min ( )
inlinestaticconstexprnoexcept

Smallest value produced.

Returns
0.

Definition at line 228 of file rng.hpp.

◆ operator()()

constexpr result_type dasmig::core::rng::operator() ( )
inlineconstexprnoexcept

Advance and return the next value.

Returns
A uniformly distributed 64-bit value.

Definition at line 158 of file rng.hpp.

◆ seed() [1/2]

constexpr std::uint64_t dasmig::core::rng::seed ( ) const
inlineconstexprnoexcept

The seed this engine was last seeded with.

Returns
The seed; constructing an engine from it replays the sequence.

Definition at line 154 of file rng.hpp.

◆ seed() [2/2]

constexpr rng & dasmig::core::rng::seed ( std::uint64_t  seed)
inlineconstexprnoexcept

Reset the engine to the start of the sequence for a seed.

Parameters
seedThe new seed.
Returns
*this.

Definition at line 137 of file rng.hpp.

◆ split() [1/2]

constexpr rng dasmig::core::rng::split ( std::string_view  label) const
inlineconstexprnoexcept

A child engine for a label, independent of this engine's state.

Parameters
labelUTF-8 label naming the child stream.
Returns
An engine seeded with child_seed(label).

Definition at line 213 of file rng.hpp.

◆ split() [2/2]

constexpr rng dasmig::core::rng::split ( std::uint64_t  index) const
inlineconstexprnoexcept

A child engine for an index, independent of this engine's state.

Parameters
indexIndex of the child stream.
Returns
An engine seeded with child_seed(index).

Definition at line 221 of file rng.hpp.

◆ unseed()

rng & dasmig::core::rng::unseed ( )
inline

Reseed from std::random_device.

Returns
*this.

Definition at line 150 of file rng.hpp.


The documentation for this class was generated from the following file: