openMSX
Classes | Functions
random.hh File Reference
#include <random>
Include dependency graph for random.hh:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  PCG< SEED >
 

Functions

auto & global_urng ()
 Return reference to a (shared) global random number generator.
 
void randomize ()
 Seed the (shared) random number generator.
 
bool random_bool ()
 Return a random boolean value.
 
int random_int (int from, int thru)
 Return a random integer in the range [from, thru] (note: closed interval).
 
float random_float (float from, float upto)
 Return a random float in the range [from, upto) (note: half-open interval).
 
uint32_t random_32bit ()
 Return a random 32-bit value.
 
constexpr float getCanonicalFloat (uint32_t u)
 

Function Documentation

◆ getCanonicalFloat()

constexpr float getCanonicalFloat ( uint32_t  u)
constexpr

Definition at line 104 of file random.hh.

◆ global_urng()

auto & global_urng ( )
inline

Return reference to a (shared) global random number generator.

Definition at line 8 of file random.hh.

Referenced by openmsx::GLSnow::GLSnow(), random_32bit(), random_bool(), random_float(), random_int(), and randomize().

◆ random_32bit()

uint32_t random_32bit ( )
inline

Return a random 32-bit value.

This function should rarely be used. It should NOT be used if you actually need random values in a smaller range than [0 .. 0xffffffff]. For example: 'random_32bit % N' with

  • N not a power-of-2: is NOT a uniform distribution anymore (higher values have a slightly lower probability than the lower values)
  • N a power-of-2: does more work than needed (typically has to call the underlying generator more than once to make sure all 32 bits are random, but then those upper bits are discarded).

Definition at line 67 of file random.hh.

References global_urng().

◆ random_bool()

bool random_bool ( )
inline

Return a random boolean value.

Definition at line 24 of file random.hh.

References global_urng().

◆ random_float()

float random_float ( float  from,
float  upto 
)
inline

Return a random float in the range [from, upto) (note: half-open interval).

This function is convenient if you only need a few random values. If you need a large amount it's a bit faster to create a local distribution object and reuse that for all your values.

Definition at line 50 of file random.hh.

References global_urng().

Referenced by openmsx::GLSnow::paint(), and openmsx::PostProcessor::rotateFrames().

◆ random_int()

int random_int ( int  from,
int  thru 
)
inline

Return a random integer in the range [from, thru] (note: closed interval).

This function is convenient if you only need a few random values. If you need a large amount it's a bit faster to create a local distribution object and reuse that for all your values.

Definition at line 38 of file random.hh.

References global_urng().

◆ randomize()

void randomize ( )
inline

Seed the (shared) random number generator.

Definition at line 16 of file random.hh.

References global_urng().