Player12
An open source football management simulator
Loading...
Searching...
No Matches
WorldRng Class Reference

Deterministic, portable pseudo random generator (xoshiro256**). More...

#include <world_rng.h>

Public Types

using result_type = std::uint64_t
 

Public Member Functions

 WorldRng (std::uint64_t seed)
 
result_type operator() ()
 
std::uint64_t next ()
 
double uniform01 ()
 
float uniform (float lo, float hi)
 
int uniformInt (int lo, int hi)
 
float normal (float mean, float stddev)
 
float lognormal (float median, float sigma)
 
bool chance (double p)
 
std::size_t weightedIndex (std::span< const float > weights)
 
template<typename T >
void shuffle (std::span< T > values)
 

Static Public Member Functions

static WorldRng stream (std::uint64_t world_seed, RngDomain domain, std::uint64_t key_a=0, std::uint64_t key_b=0)
 
static double hashUniform (std::uint64_t world_seed, RngDomain domain, std::uint64_t key_a, std::uint64_t key_b=0)
 
static constexpr result_type min ()
 
static constexpr result_type max ()
 

Private Attributes

std::array< std::uint64_t, 4 > state {}
 

Detailed Description

Deterministic, portable pseudo random generator (xoshiro256**).

All distributions are implemented here instead of using <random> distributions, whose algorithms differ between standard libraries. The same seed therefore produces the same world on every platform.

Member Typedef Documentation

◆ result_type

using WorldRng::result_type = std::uint64_t

Constructor & Destructor Documentation

◆ WorldRng()

WorldRng::WorldRng ( std::uint64_t  seed)
explicit

Seeds the four state words from seed through splitmix64.

Member Function Documentation

◆ chance()

bool WorldRng::chance ( double  p)

Bernoulli trial with probability p.

◆ hashUniform()

double WorldRng::hashUniform ( std::uint64_t  world_seed,
RngDomain  domain,
std::uint64_t  key_a,
std::uint64_t  key_b = 0 
)
static

Single uniform draw in [0, 1) without constructing a stream.

◆ lognormal()

float WorldRng::lognormal ( float  median,
float  sigma 
)

Lognormal value with the given median and log-space sigma.

◆ max()

static constexpr result_type WorldRng::max ( )
inlinestaticconstexpr

◆ min()

static constexpr result_type WorldRng::min ( )
inlinestaticconstexpr

◆ next()

std::uint64_t WorldRng::next ( )

Next raw 64-bit value.

◆ normal()

float WorldRng::normal ( float  mean,
float  stddev 
)

Normally distributed value (Box-Muller over PortableMath).

◆ operator()()

result_type WorldRng::operator() ( )
inline

◆ shuffle()

template<typename T >
void WorldRng::shuffle ( std::span< T >  values)
inline

Fisher-Yates shuffle with this generator.

◆ stream()

WorldRng WorldRng::stream ( std::uint64_t  world_seed,
RngDomain  domain,
std::uint64_t  key_a = 0,
std::uint64_t  key_b = 0 
)
static

Creates the stream for domain keyed by up to two stable values.

◆ uniform()

float WorldRng::uniform ( float  lo,
float  hi 
)

Uniform float in [lo, hi).

◆ uniform01()

double WorldRng::uniform01 ( )

Uniform double in [0, 1).

◆ uniformInt()

int WorldRng::uniformInt ( int  lo,
int  hi 
)

Uniform integer in [lo, hi] (inclusive, unbiased).

◆ weightedIndex()

std::size_t WorldRng::weightedIndex ( std::span< const float >  weights)

Index drawn proportionally to non-negative weights.

Member Data Documentation

◆ state

std::array<std::uint64_t, 4> WorldRng::state {}
private

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