openMSX
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.

Functions

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

Function Documentation

◆ 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(), openmsx::SDLSnow< Pixel >::paint(), random_bool(), randomize(), openmsx::FBPostProcessor< Pixel >::rotateFrames(), and openmsx::GLPostProcessor::update().

◆ 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 uniform distribution anymore (higher values have a slighly 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.

Referenced by openmsx::DiskImageUtils::checkFAT12Partition().

◆ 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.

Referenced by openmsx::GLSnow::paint(), and openmsx::GLPostProcessor::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.

◆ randomize()

void randomize ( )
inline

Seed the (shared) random number generator.

Definition at line 16 of file random.hh.

References global_urng().