SOFA API  1c8b807d
Open source framework for multi-physics simuation
sofa::testing::LinearCongruentialRandomGenerator Class Reference

#include <LinearCongruentialRandomGenerator.h>

A simple deterministic and portable random number generator. More...

Detailed Description

A simple deterministic and portable random number generator.

This class implements a Linear Congruential Generator (LCG) algorithm to generate pseudo-random numbers. It is designed to provide deterministic and portable random number generation, making it well-suited for testing purposes.

Public Member Functions

 LinearCongruentialRandomGenerator (unsigned int initialSeed)
 
unsigned int generateRandom ()
 Generates the next pseudo-random number. More...
 
template<class Scalar >
Scalar generateInUnitRange ()
 Generates a pseudo-random value within the unit interval [0, 1]. More...
 
double generateInRange (double rmin, double rmax)
 Generates a pseudo-random double value within a specified range. More...
 
float generateInRange (float rmin, float rmax)
 Generates a pseudo-random float value within a specified range. More...
 

Constructor details

◆ LinearCongruentialRandomGenerator()

sofa::testing::LinearCongruentialRandomGenerator::LinearCongruentialRandomGenerator ( unsigned int  initialSeed)
explicit

Function details

◆ generateInRange() [1/2]

double sofa::testing::LinearCongruentialRandomGenerator::generateInRange ( double  rmin,
double  rmax 
)

Generates a pseudo-random double value within a specified range.

This function generates a pseudo-random double value between the provided minimum (rmin) and maximum (rmax) values.

Parameters
rminThe minimum value of the desired range (inclusive).
rmaxThe maximum value of the desired range (inclusive).
Returns
A pseudo-random double value in the specified range [rmin, rmax].

Example usage:

double randomValue = generateInRange(10.0, 20.0);
double generateInRange(double rmin, double rmax)
Generates a pseudo-random double value within a specified range.
Definition: LinearCongruentialRandomGenerator.cpp:41

◆ generateInRange() [2/2]

float sofa::testing::LinearCongruentialRandomGenerator::generateInRange ( float  rmin,
float  rmax 
)

Generates a pseudo-random float value within a specified range.

This function generates a pseudo-random float value between the provided minimum (rmin) and maximum (rmax) values.

Parameters
rminThe minimum value of the desired range (inclusive).
rmaxThe maximum value of the desired range (inclusive).
Returns
A pseudo-random float value in the specified range [rmin, rmax].

Example usage:

float randomValue = generateInRange(10.f, 20.f);

◆ generateInUnitRange()

template<class Scalar >
Scalar sofa::testing::LinearCongruentialRandomGenerator::generateInUnitRange ( )
inline

Generates a pseudo-random value within the unit interval [0, 1].

This templated function generates a pseudo-random value of the specified scalar type within the unit interval [0, 1]. It utilizes the underlying random number generator to produce a normalized random value within the unit range.

Template Parameters
ScalarThe scalar type for the generated value (e.g., float, double).
Returns
A pseudo-random value of the specified scalar type within the range [0, 1].

Example usage:

float randomFloat = generateInUnitRange<float>();
double randomDouble = generateInUnitRange<double>();

◆ generateRandom()

unsigned sofa::testing::LinearCongruentialRandomGenerator::generateRandom ( )

Generates the next pseudo-random number.

Returns
The generated pseudo-random number.

This method uses a Linear Congruential Generator (LCG) algorithm to update the seed and produce the next pseudo-random number.