Cbeam
Loading...
Searching...
No Matches
cbeam::random Namespace Reference

Collects random number generation tools for multithreaded environments. It includes a default thread-local generator and utilities to produce random integers, uniform distributions, or random strings. This promotes convenient generation of secure or test-oriented random values without external dependencies. More...

Functions

std::mt19937 & default_generator ()
 Returns a reference to a thread-local std::mt19937 random number generator.
 
std::size_t random_number (const std::size_t n, std::mt19937 &gen=default_generator())
 Returns a random number in the range [0, n-1].
 
std::string random_string (std::string::size_type length, std::mt19937 &gen=default_generator())
 Generates a random string of specified length.
 

Detailed Description

Collects random number generation tools for multithreaded environments. It includes a default thread-local generator and utilities to produce random integers, uniform distributions, or random strings. This promotes convenient generation of secure or test-oriented random values without external dependencies.

Function Documentation

◆ default_generator()

std::mt19937 & cbeam::random::default_generator ( )
inline

Returns a reference to a thread-local std::mt19937 random number generator.

This function creates a thread-local std::mt19937 random number generator if it doesn't already exist, and returns a reference to it. The generator is initialized with std::random_device upon first use, ensuring good initial randomness.

Returns
Reference to the thread-local std::mt19937 generator.

Thread-local generator: Each thread gets its own instance of the generator. This ensures independence between threads and thread-safe random number generation.

◆ random_number()

std::size_t cbeam::random::random_number ( const std::size_t n,
std::mt19937 & gen = default_generator() )
inline

Returns a random number in the range [0, n-1].

Parameters
nThe upper bound (exclusive) for the random number.
genReference to a std::mt19937 random number generator, defaulting to the thread-local generator.
Returns
A random number between 0 and n-1.

◆ random_string()

std::string cbeam::random::random_string ( std::string::size_type length,
std::mt19937 & gen = default_generator() )
inline

Generates a random string of specified length.

This function creates a random string consisting of alphanumeric characters (both lowercase and uppercase) and digits. It uses a thread-local std::uniform_int_distribution to pick characters uniformly from the character set. This ensures efficient and thread-safe generation of random strings.

Parameters
lengthThe desired length of the random string.
genA reference to a std::mt19937 random number generator, defaulting to the thread-local generator.
Returns
A random string of the specified length.