Skip to main content

Random Class

Utility class for generating pseudo-random numbers. More...

Declaration

class helios::util::Random { ... }

Public Constructors Index

Random (const uint32_t seed)

Initializes the generator with the provided seed value. More...

Public Member Functions Index

voidreset ()

Resets the generator to its initial state. More...

floatrandomFloat (float a, float b) noexcept

Generates a pseudo-random float in the range [a, b). More...

Private Member Attributes Index

std::mt19937gen_

The Mersenne Twister pseudo-random number generator. More...

std::uniform_real_distribution< float >uniformDist_

Uniform real distribution for generating floating-point numbers. More...

uint32_tinitialSeed_

The initial seed used to initialize the generator. More...

Description

Utility class for generating pseudo-random numbers.

This class provides a random number generator based on the Mersenne Twister algorithm (`std::mt19937`).

Example usage: ```cpp auto rng = helios::util::Random(12345); float value = rng.randomFloat(0.0f, 1.0f); ```

Definition at line 27 of file Random.ixx.

Public Constructors

Random()

helios::util::Random::Random (const uint32_t seed)
inline explicit

Initializes the generator with the provided seed value.

Parameters
seed

The seed value for the random number generator.

Definition at line 58 of file Random.ixx.

58 explicit Random(const uint32_t seed) : gen_(seed), initialSeed_(seed) {};

Public Member Functions

randomFloat()

float helios::util::Random::randomFloat (float a, float b)
inline nodiscard noexcept

Generates a pseudo-random float in the range [a, b).

Uses `std::uniform_real_distribution` to generate a uniformly distributed floating-point number.

Parameters
a

The lower bound of the range (inclusive).

b

The upper bound of the range (exclusive).

Returns

A random float value in the range [a, b).

See Also

https://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution

Definition at line 85 of file Random.ixx.

85 [[nodiscard]] float randomFloat(float a, float b) noexcept {
86
87 if (b < a) {
88 auto tmp = a; a = b; b = tmp;
89 }
90
91 if (a == b) {
92 return a;
93 }
94
95 return uniformDist_(
96 gen_,
97 std::uniform_real_distribution<float>::param_type{a, b}
98 );
99 }

Referenced by helios::engine::runtime::spawn::behavior::placements::ColumnSpawnPlacer::getPosition.

reset()

void helios::util::Random::reset ()
inline

Resets the generator to its initial state.

Re-seeds the generator with the original seed value and resets the distribution state. Useful for reproducible sequences.

Definition at line 66 of file Random.ixx.

66 void reset() {
67 gen_.seed(initialSeed_);
68
69 uniformDist_.reset();
70 }

Private Member Attributes

gen_

std::mt19937 helios::util::Random::gen_

The Mersenne Twister pseudo-random number generator.

Definition at line 34 of file Random.ixx.

34 std::mt19937 gen_;

initialSeed_

uint32_t helios::util::Random::initialSeed_

The initial seed used to initialize the generator.

Stored for use by `reset()` to restore the generator to its initial state.

Definition at line 49 of file Random.ixx.

49 uint32_t initialSeed_;

uniformDist_

std::uniform_real_distribution<float> helios::util::Random::uniformDist_

Uniform real distribution for generating floating-point numbers.

See Also

https://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution

Definition at line 41 of file Random.ixx.

41 std::uniform_real_distribution<float> uniformDist_;

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.