Rendu
A lightweight rendering engine for experimentations
Loading...
Searching...
No Matches
Classes | Static Public Member Functions | Static Private Attributes | List of all members
Random Class Reference

Generate seedable random numbers of various types and in multiple intervals. Handles per-thread random number generators. More...

#include <Random.hpp>

Classes

struct  LocalMT19937
 A MT19937 generator seeded using the shared generator. Used to provide per-thread MT19937 generators in a thread-safe way. More...
 

Static Public Member Functions

static void seed ()
 
static void seed (unsigned int seedValue)
 
static unsigned int getSeed ()
 
static int Int (int min, int max)
 
static float Float ()
 
static float Float (float min, float max)
 
static glm::vec3 Color ()
 
static glm::vec2 sampleDisk ()
 
static glm::vec3 sampleSphere ()
 
static glm::vec3 sampleCosineHemisphere ()
 
template<typename T >
static void shuffle (std::vector< T > &items)
 

Static Private Attributes

static unsigned int _seed
 The current main seed.
 
static std::mt19937 _shared
 Shared randomness generator, used for seeding per-thread generators. More...
 
static std::mutex _lock
 The lock for the shared generator.
 
static thread_local LocalMT19937 _thread
 Per-thread randomness generator, seeded using the shared generator.
 

Detailed Description

Generate seedable random numbers of various types and in multiple intervals. Handles per-thread random number generators.

Member Function Documentation

◆ Color()

glm::vec3 Random::Color ( )
static

Generate a random color in RGB color space. Internally a HSV color with reasonable saturation and luminance is generated and converted to RGB.

Returns
an RGB triplet.

◆ Float() [1/2]

float Random::Float ( )
static

Generate a float in [0.0, 1.0)

Returns
a float in [0.0, 1.0)

◆ Float() [2/2]

float Random::Float ( float  min,
float  max 
)
static

Generate a float in a given interval.

Parameters
minthe included lower bound
maxthe excluded higher bound
Returns
a float in [min, max)

◆ getSeed()

unsigned int Random::getSeed ( )
static

Query the current global seed.

Returns
the current global seed

◆ Int()

int Random::Int ( int  min,
int  max 
)
static

Generate an integer in a given interval.

Parameters
minthe included lower bound
maxthe included higher bound
Returns
an integer in [min, max]

◆ sampleCosineHemisphere()

glm::vec3 Random::sampleCosineHemisphere ( )
static

Sample point from the hemisphere, following a cosine lobe

Returns
a 3D point on the unit z-positive hemisphere

◆ sampleDisk()

glm::vec2 Random::sampleDisk ( )
static

Sample point uniformly on a disk.

Returns
a 2D point on the unit disk

◆ sampleSphere()

glm::vec3 Random::sampleSphere ( )
static

Sample point uniformly on a sphere.

Returns
a 3D point on the unit sphere

◆ seed() [1/2]

void Random::seed ( )
static

Seed the shared generator using a random number.

Note
The seed is obtained through a std::random_device.
Warning
Threads created before the call won't be seeded (except for the calling thread).
Note
It is recommended to seed the generator on the main thread at the beginning of the application execution.

◆ seed() [2/2]

void Random::seed ( unsigned int  seedValue)
static

Seed the shared generator using a given number.

Parameters
seedValuethe seed to use
Warning
Threads created before the call won't be seeded (except for the calling thread).
Note
It is recommended to seed the generator on the main thread at the beginning of the application execution.

◆ shuffle()

template<typename T >
void Random::shuffle ( std::vector< T > &  items)
static

Shuffle elements of a vector randomly, in-place.

Parameters
itemsthe items to shuffle

Member Data Documentation

◆ _shared

std::mt19937 Random::_shared
staticprivate

Shared randomness generator, used for seeding per-thread generators.

Warning
Not thread safe.

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