CCIMXDesktop
 
Loading...
Searching...
No Matches
RandomGenerator< RandType > Class Template Reference

A templated class for generating random numbers of a specified arithmetic type. More...

#include <random_gen_value.h>

Static Public Member Functions

static RandType random (const RandType min, const RandType max)
 Compile-time assertion to ensure RandType is an arithmetic type. If RandType is not arithmetic (e.g., a custom class), a compilation error will occur.
 
template<int MAX_TRY = 100>
static RandType random_except (const RandType min, const RandType max, const RandType except)
 Generates a random number within a specified range, excluding a particular value.
 

Detailed Description

template<typename RandType>
class RandomGenerator< RandType >

A templated class for generating random numbers of a specified arithmetic type.

This class provides static methods to generate random numbers within a given range, supporting both integral and floating-point types. It also includes a method to generate a random number while excluding a specific value.

Template Parameters
RandTypeThe type of random number to generate. Must be an arithmetic type.

Member Function Documentation

◆ random()

template<typename RandType >
static RandType RandomGenerator< RandType >::random ( const RandType  min,
const RandType  max 
)
inlinestatic

Compile-time assertion to ensure RandType is an arithmetic type. If RandType is not arithmetic (e.g., a custom class), a compilation error will occur.

Generates a random number within a specified range [min, max].

  • This method uses std::random_device for non-deterministic seeding and std::mt19937 as the random number engine. It employs std::uniform_int_distribution for integral types and std::uniform_real_distribution for floating-point types to ensure uniform distribution.
  • Parameters
    minThe minimum possible value in the random range (inclusive).
    maxThe maximum possible value in the random range (inclusive).
    Returns
    A randomly generated value of type RandType within the specified range.
    Exceptions
    std::logic_errorif RandType is not an integral or floating-point type (though static_assert should catch this at compile time).

< Non-deterministic random number generator.

< Mersenne Twister engine seeded by rd.

< Distribution for integral types.

< Non-deterministic random number generator.

< Mersenne Twister engine seeded by rd.

< Distribution for floating-point types.

◆ random_except()

template<typename RandType >
template<int MAX_TRY = 100>
static RandType RandomGenerator< RandType >::random_except ( const RandType  min,
const RandType  max,
const RandType  except 
)
inlinestatic

Generates a random number within a specified range, excluding a particular value.

  • This method repeatedly calls random(min, max) up to MAX_TRY times until a value different from except is generated.
  • Template Parameters
    MAX_TRYThe maximum number of attempts to generate a value different from except. Defaults to 100.
    Parameters
    minThe minimum possible value in the random range (inclusive).
    maxThe maximum possible value in the random range (inclusive).
    exceptThe value to be excluded from the generated random numbers.
    Returns
    A randomly generated value of type RandType within the specified range, not equal to except.
    Exceptions
    std::logic_errorif the range [min, max] only contains the except value, making generation impossible.
    std::runtime_errorif a value different from except cannot be generated within MAX_TRY attempts.

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