Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::core::Random Class Reference

Seeded pseudo-random generator (PCG32), for gameplay the server must replay. More...

#include <aegis2d/core/random.hpp>

Public Types

using result_type = std::uint32_t
 Type of the raw numbers.
 

Public Member Functions

constexpr Random (std::uint64_t seed, std::uint64_t stream=0) noexcept
 Starts a sequence.
 
constexpr result_type next () noexcept
 Next raw number.
 
constexpr result_type operator() () noexcept
 Next raw number.
 
constexpr std::uint32_t below (std::uint32_t bound) noexcept
 Whole number below a bound, every value equally likely.
 
constexpr float uniform () noexcept
 Real number between 0 and 1.
 
constexpr float uniform (float low, float high) noexcept
 Real number between two bounds.
 

Static Public Member Functions

static constexpr result_type min () noexcept
 Smallest raw number.
 
static constexpr result_type max () noexcept
 Largest raw number.
 

Detailed Description

Seeded pseudo-random generator (PCG32), for gameplay the server must replay.

A given seed and stream always give the same numbers, on every compiler and standard library: unlike std::uniform_int_distribution and std::uniform_real_distribution, whose results differ between libstdc++, libc++ and MSVC, below() and uniform() are defined here. 16 bytes of state, fast, good statistical quality, but not for cryptography. It meets std::uniform_random_bit_generator, so it also works with std::shuffle, whose results then depend on the standard library.

Member Typedef Documentation

◆ result_type

using aegis2d::core::Random::result_type = std::uint32_t

Type of the raw numbers.

Constructor & Destructor Documentation

◆ Random()

constexpr aegis2d::core::Random::Random ( std::uint64_t  seed,
std::uint64_t  stream = 0 
)
inlineexplicitconstexprnoexcept

Starts a sequence.

Parameters
seedPicks the starting point: the same seed gives the same numbers.
streamPicks one of 2^63 independent sequences, e.g. one per system, so that drawing more numbers in one does not change the others.

Member Function Documentation

◆ below()

constexpr std::uint32_t aegis2d::core::Random::below ( std::uint32_t  bound)
inlineconstexprnoexcept

Whole number below a bound, every value equally likely.

Parameters
boundNumber of possible values.
Returns
A number in [0, bound), or 0 when bound is 0.

◆ max()

static constexpr result_type aegis2d::core::Random::max ( )
inlinestaticconstexprnoexcept

Largest raw number.

Returns
2^32 - 1.

◆ min()

static constexpr result_type aegis2d::core::Random::min ( )
inlinestaticconstexprnoexcept

Smallest raw number.

Returns
0.

◆ next()

constexpr result_type aegis2d::core::Random::next ( )
inlineconstexprnoexcept

Next raw number.

Returns
32 random bits.

◆ operator()()

constexpr result_type aegis2d::core::Random::operator() ( )
inlineconstexprnoexcept

Next raw number.

Returns
32 random bits.

◆ uniform() [1/2]

constexpr float aegis2d::core::Random::uniform ( )
inlineconstexprnoexcept

Real number between 0 and 1.

Returns
A float in [0, 1), with the 24 bits of precision a float has.

◆ uniform() [2/2]

constexpr float aegis2d::core::Random::uniform ( float  low,
float  high 
)
inlineconstexprnoexcept

Real number between two bounds.

Parameters
lowOne bound.
highThe other bound.
Returns
low + (high - low) * uniform(): from low to high, every value equally likely.

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