Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
helios::RandomGenerator< Engine > Class Template Reference

Random number utilities with a user-provided engine. More...

#include <random.hpp>

Public Member Functions

 RandomGenerator (Engine &engine) noexcept
 Constructs a RandomGenerator from an existing engine reference.
 
 RandomGenerator (const RandomGenerator &) noexcept=default
 
 RandomGenerator (RandomGenerator &&) noexcept=default
 
 ~RandomGenerator () noexcept=default
 
RandomGeneratoroperator= (const RandomGenerator &) noexcept=default
 
RandomGeneratoroperator= (RandomGenerator &&) noexcept=default
 
template<typename Dist >
requires Distribution<Dist, Engine>
auto Next (Dist &dist) noexcept(std::is_nothrow_invocable_v< Dist, Engine & >) -> typename Dist::result_type
 Generates a value using the provided distribution.
 
template<utils::ArithmeticTrait T>
Value ()
 Generates a random arithmetic value using a reasonable default distribution.
 
template<utils::ArithmeticTrait T, utils::ArithmeticTrait U>
auto ValueFromRange (T min, U max) -> std::common_type_t< T, U >
 Generates a random arithmetic value within the specified range.
 
Engine & EngineRef () const noexcept
 Provides access to the underlying engine.
 

Detailed Description

template<RandomEngine Engine>
class helios::RandomGenerator< Engine >

Random number utilities with a user-provided engine.

This wrapper delegates all random generation to an underlying engine instance supplied by the user. It never owns the engine and does not perform any static initialization of engines itself.

Template Parameters
EngineRandomEngine type used for generation.

Definition at line 123 of file random.hpp.

Constructor & Destructor Documentation

◆ RandomGenerator() [1/3]

template<RandomEngine Engine>
helios::RandomGenerator< Engine >::RandomGenerator ( Engine &  engine)
inlineexplicitnoexcept

Constructs a RandomGenerator from an existing engine reference.

The engine is not owned and must outlive this object.

Parameters
engineReference to engine used for random generation.

Definition at line 130 of file random.hpp.

130: engine_(engine) {}

◆ RandomGenerator() [2/3]

template<RandomEngine Engine>
helios::RandomGenerator< Engine >::RandomGenerator ( const RandomGenerator< Engine > &  )
defaultnoexcept

◆ RandomGenerator() [3/3]

template<RandomEngine Engine>
helios::RandomGenerator< Engine >::RandomGenerator ( RandomGenerator< Engine > &&  )
defaultnoexcept

◆ ~RandomGenerator()

template<RandomEngine Engine>
helios::RandomGenerator< Engine >::~RandomGenerator ( )
defaultnoexcept

Member Function Documentation

◆ EngineRef()

template<RandomEngine Engine>
Engine & helios::RandomGenerator< Engine >::EngineRef ( ) const
inlinenoexcept

Provides access to the underlying engine.

Returns
Reference to the engine used by this generator.

Definition at line 182 of file random.hpp.

182{ return engine_.get(); }

◆ Next()

template<RandomEngine Engine>
template<typename Dist >
requires Distribution<Dist, Engine>
auto helios::RandomGenerator< Engine >::Next ( Dist &  dist) -> typename Dist::result_type
inlinenoexcept

Generates a value using the provided distribution.

This is a low-level interface that accepts an arbitrary distribution object. Intended for cases where caller needs full control over distribution parameters.

Template Parameters
DistDistribution type compatible with Engine.
Parameters
distDistribution instance used for generation.
Returns
Generated random value of type Dist::result_type.

Definition at line 148 of file random.hpp.

149 {
150 return dist(engine_.get());
151 }

◆ operator=() [1/2]

template<RandomEngine Engine>
RandomGenerator & helios::RandomGenerator< Engine >::operator= ( const RandomGenerator< Engine > &  )
defaultnoexcept

◆ operator=() [2/2]

template<RandomEngine Engine>
RandomGenerator & helios::RandomGenerator< Engine >::operator= ( RandomGenerator< Engine > &&  )
defaultnoexcept

◆ Value()

template<RandomEngine Engine>
template<utils::ArithmeticTrait T>
T helios::RandomGenerator< Engine >::Value ( )
inline

Generates a random arithmetic value using a reasonable default distribution.

For integral types, uses std::uniform_int_distribution over the full representable range, except for bool which uses a uniform {false, true}. For floating point types, uses std::uniform_real_distribution in the [0, 1) range to avoid dependence on std::numeric_limits<>::min().

Template Parameters
TArithmetic type to generate.
Returns
Randomly generated value of type T.

Definition at line 190 of file random.hpp.

190 {
191 Engine& engine = engine_.get();
192 if constexpr (std::integral<T>) {
193 if constexpr (std::same_as<T, bool>) {
194 std::uniform_int_distribution<int> dist(0, 1);
195 return dist(engine) == 1;
196 } else {
197 std::uniform_int_distribution<T> dist(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
198 return dist(engine);
199 }
200 } else {
201 std::uniform_real_distribution<T> dist(static_cast<T>(0), static_cast<T>(1));
202 return dist(engine);
203 }
204}

◆ ValueFromRange()

template<RandomEngine Engine>
template<utils::ArithmeticTrait T, utils::ArithmeticTrait U>
auto helios::RandomGenerator< Engine >::ValueFromRange ( min,
max 
) -> std::common_type_t<T, U>
inline

Generates a random arithmetic value within the specified range.

For integral types, uses std::uniform_int_distribution with closed interval [min, max]. For floating point types, uses std::uniform_real_distribution with interval [min, max).

Template Parameters
TArithmetic type.
UArithmetic type.
Parameters
minLower bound of the range.
maxUpper bound of the range.
Returns
Random value of common_type_t<T, U> within [min, max] or [min, max).

Definition at line 208 of file random.hpp.

208 {
209 using Common = std::common_type_t<T, U>;
210 const auto cmin = static_cast<Common>(min);
211 const auto cmax = static_cast<Common>(max);
212
213 Engine& engine = engine_.get();
214 if constexpr (std::integral<Common>) {
215 using DistType = std::conditional_t<std::signed_integral<Common>, int64_t, uint64_t>;
216 std::uniform_int_distribution<DistType> dist(static_cast<DistType>(cmin), static_cast<DistType>(cmax));
217 return static_cast<Common>(dist(engine));
218 } else {
219 std::uniform_real_distribution<Common> dist(cmin, cmax);
220 return dist(engine);
221 }
222}