cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Static Protected Attributes | List of all members
cmnRandomSequence Class Reference

Provide an interface to a reproducible random sequence. More...

#include <cmnRandomSequence.h>

Public Types

typedef unsigned int SeedType
 
typedef int ElementaryRandomNumber
 
typedef unsigned long SequenceCounterType
 

Public Member Functions

SeedType GetSeed (void) const
 
void SetSeed (const SeedType seed)
 
SequenceCounterType GetSequencePosition (void) const
 
void SetSequencePosition (const SequenceCounterType position)
 
ElementaryRandomNumber ExtractRandomElement (void)
 
float ExtractRandomFloat (void)
 
float ExtractRandomFloat (const float min, const float max)
 
void ExtractRandomFloatArray (const float min, const float max, float *array, const size_t arraySize)
 
double ExtractRandomDouble (void)
 
double ExtractRandomDouble (const double min, const double max)
 
void ExtractRandomDoubleArray (const double min, const double max, double *array, const size_t arraySize)
 
int ExtractRandomInt (void)
 
int ExtractRandomInt (const int min, const int max)
 
void ExtractRandomIntArray (const int min, const int max, int *array, const size_t arraySize)
 
unsigned int ExtractRandomUnsignedInt (void)
 
unsigned int ExtractRandomUnsignedInt (const unsigned int min, const unsigned int max)
 
void ExtractRandomUnsignedIntArray (const unsigned int min, const unsigned int max, unsigned int *array, const size_t arraySize)
 
short ExtractRandomShort (void)
 
short ExtractRandomShort (const short min, const short max)
 
void ExtractRandomShortArray (const short min, const short max, short *array, const size_t arraySize)
 
unsigned short ExtractRandomUnsignedShort (void)
 
unsigned short ExtractRandomUnsignedShort (const unsigned short min, const unsigned short max)
 
void ExtractRandomUnsignedShortArray (const unsigned short min, const unsigned short max, unsigned short *array, const size_t arraySize)
 
long ExtractRandomLong (void)
 
long ExtractRandomLong (const long min, const long max)
 
void ExtractRandomLongArray (const long min, const long max, long *array, const size_t arraySize)
 
unsigned long ExtractRandomUnsignedLong (void)
 
unsigned long ExtractRandomUnsignedLong (const unsigned long min, const unsigned long max)
 
void ExtractRandomUnsignedLongArray (const unsigned long min, const unsigned long max, unsigned long *array, const size_t arraySize)
 
long long ExtractRandomLongLong (void)
 
long ExtractRandomLongLong (const long long min, const long long max)
 
void ExtractRandomLongLongArray (const long long min, const long long max, long long *array, const size_t arraySize)
 
unsigned long long ExtractRandomUnsignedLongLong (void)
 
unsigned long long ExtractRandomUnsignedLongLong (const unsigned long long min, const unsigned long long max)
 
void ExtractRandomUnsignedLongLongArray (const unsigned long long min, const unsigned long long max, unsigned long long *array, const size_t arraySize)
 
char ExtractRandomChar (void)
 
char ExtractRandomChar (const char min, const char max)
 
void ExtractRandomCharArray (const char min, const char max, char *array, const size_t arraySize)
 
unsigned char ExtractRandomUnsignedChar (void)
 
unsigned char ExtractRandomUnsignedChar (const unsigned char min, const unsigned char max)
 
void ExtractRandomUnsignedCharArray (const unsigned char min, const unsigned char max, unsigned char *array, const size_t arraySize)
 
void ExtractRandomPermutation (const size_t length, size_t *array)
 
template<typename _valueType >
void ExtractRandomValue (_valueType &result)
 
template<typename _valueType >
void ExtractRandomValue (const _valueType min, const _valueType max, _valueType &result)
 
template<typename _valueType >
void ExtractRandomValueArray (const _valueType min, const _valueType max, _valueType *array, const size_t arraySize)
 
size_t ExtractRandomSizeT (const size_t min, const size_t max)
 
void ExtractRandomSizeTArray (const size_t min, const size_t max, size_t *array, const size_t arraySize)
 
ptrdiff_t ExtractRandomPtrdiffT (const ptrdiff_t min, const ptrdiff_t max)
 
void ExtractRandomPtrdiffTArray (const ptrdiff_t min, const ptrdiff_t max, ptrdiff_t *array, const size_t arraySize)
 

Static Public Member Functions

static cmnRandomSequenceGetInstance (void)
 

Static Public Attributes

static const SeedType DefaultSeed
 
static const ElementaryRandomNumber LowerRandomBound
 
static const ElementaryRandomNumber UpperRandomBound
 

Protected Member Functions

 cmnRandomSequence ()
 
 cmnRandomSequence (const SeedType seed, const SequenceCounterType position=0)
 
 cmnRandomSequence (const cmnRandomSequence &other)
 
cmnRandomSequenceoperator= (const cmnRandomSequence &other)
 

Static Protected Attributes

static cmnRandomSequence RandomInstance
 

Detailed Description

Provide an interface to a reproducible random sequence.

Class cmnRandomSequence provides a reproducible random sequence. A random sequence is defined as a sequence of elements of type ElementaryRandomNumber, which is generated by a more or less good random number generator. The random number generator is initialized with a seed, and outputs a sequence of numbers.

In order to reproduce a random sequence, we need to store the seed and the position in the sequence. We can then resume to any position in the sequence using the method SetSequencePosition().

Important note: In the current implementation, we are using the rand() function of the standard C/C++ library. However, as the rand() function does not have a cross-platform standardized behavior, our random sequence is only reproducible within the scope of one compiler on one machine. In addition, as we depend on a global mechanism, all the random sequences use the same resource. This means that in fact, there is only one random sequence in the system. Therefore, the current implementation is as a Singleton.

Useful background on randomization can be found, for example, in Numerical Recipes: http://www.nr.com . Due to copyright issues, we are currently delaying its use. Another possible source for randomization functions is in the Gnu Scientific Library (GSL): http://www.gnu.org/software/gsl/gsl.html .

Member Typedef Documentation

type of the elementary random number that's generated by cmnRandomSequence

typedef unsigned int cmnRandomSequence::SeedType

type of the randomization seed

type of counter of the number of elements that were extracted from the sequence

Constructor & Destructor Documentation

cmnRandomSequence::cmnRandomSequence ( )
inlineprotected

Default constructor – initialize the randomization seed to the default seed and start at the first element of the random sequence.

cmnRandomSequence::cmnRandomSequence ( const SeedType  seed,
const SequenceCounterType  position = 0 
)
inlineprotected

Parametrized constructor – initialize the randomization seed by a parameter and start at the specified position (zero based) of the random sequence

cmnRandomSequence::cmnRandomSequence ( const cmnRandomSequence other)
inlineprotected

Copy constructor – initialize the seed and the sequence position identically to the other object

Member Function Documentation

char cmnRandomSequence::ExtractRandomChar ( void  )
inline
char cmnRandomSequence::ExtractRandomChar ( const char  min,
const char  max 
)
inline
void cmnRandomSequence::ExtractRandomCharArray ( const char  min,
const char  max,
char *  array,
const size_t  arraySize 
)
inline
double cmnRandomSequence::ExtractRandomDouble ( void  )
inline

Return a random double-precision floating point number in the range (0..1)

double cmnRandomSequence::ExtractRandomDouble ( const double  min,
const double  max 
)
inline

Return a random double-precision floating point number in the range [min..max]

void cmnRandomSequence::ExtractRandomDoubleArray ( const double  min,
const double  max,
double *  array,
const size_t  arraySize 
)
inline
ElementaryRandomNumber cmnRandomSequence::ExtractRandomElement ( void  )
inline

Extract a single random element from the sequence. This is the basic operation on the random sequence.

float cmnRandomSequence::ExtractRandomFloat ( void  )
inline

Return a random floating point number in the range (0..1)

float cmnRandomSequence::ExtractRandomFloat ( const float  min,
const float  max 
)
inline

Return a random floating point number in the range [min..max]

void cmnRandomSequence::ExtractRandomFloatArray ( const float  min,
const float  max,
float *  array,
const size_t  arraySize 
)
inline
int cmnRandomSequence::ExtractRandomInt ( void  )
inline
int cmnRandomSequence::ExtractRandomInt ( const int  min,
const int  max 
)
inline
void cmnRandomSequence::ExtractRandomIntArray ( const int  min,
const int  max,
int *  array,
const size_t  arraySize 
)
inline
long cmnRandomSequence::ExtractRandomLong ( void  )
inline
long cmnRandomSequence::ExtractRandomLong ( const long  min,
const long  max 
)
inline
void cmnRandomSequence::ExtractRandomLongArray ( const long  min,
const long  max,
long *  array,
const size_t  arraySize 
)
inline
long long cmnRandomSequence::ExtractRandomLongLong ( void  )
inline
long cmnRandomSequence::ExtractRandomLongLong ( const long long  min,
const long long  max 
)
inline
void cmnRandomSequence::ExtractRandomLongLongArray ( const long long  min,
const long long  max,
long long *  array,
const size_t  arraySize 
)
inline
void cmnRandomSequence::ExtractRandomPermutation ( const size_t  length,
size_t *  array 
)

Fill the given array with a random permutation of the numbers 0..length

ptrdiff_t cmnRandomSequence::ExtractRandomPtrdiffT ( const ptrdiff_t  min,
const ptrdiff_t  max 
)
inline
void cmnRandomSequence::ExtractRandomPtrdiffTArray ( const ptrdiff_t  min,
const ptrdiff_t  max,
ptrdiff_t *  array,
const size_t  arraySize 
)
inline
short cmnRandomSequence::ExtractRandomShort ( void  )
inline
short cmnRandomSequence::ExtractRandomShort ( const short  min,
const short  max 
)
inline
void cmnRandomSequence::ExtractRandomShortArray ( const short  min,
const short  max,
short *  array,
const size_t  arraySize 
)
inline
size_t cmnRandomSequence::ExtractRandomSizeT ( const size_t  min,
const size_t  max 
)
inline

Specialized versions for size_t and ptrdiff_t. We have to create new methods for these types instead of overloading since some compilers don't define these as native types but typedefs. If these types are typdefs, it is not possible to overload methods or specialize templates as these would conflict with the native types int 32/64 singed/unsigned based on OS/Compiler data model (i.e. LP64 or LLP64).

void cmnRandomSequence::ExtractRandomSizeTArray ( const size_t  min,
const size_t  max,
size_t *  array,
const size_t  arraySize 
)
inline
unsigned char cmnRandomSequence::ExtractRandomUnsignedChar ( void  )
inline
unsigned char cmnRandomSequence::ExtractRandomUnsignedChar ( const unsigned char  min,
const unsigned char  max 
)
inline
void cmnRandomSequence::ExtractRandomUnsignedCharArray ( const unsigned char  min,
const unsigned char  max,
unsigned char *  array,
const size_t  arraySize 
)
inline
unsigned int cmnRandomSequence::ExtractRandomUnsignedInt ( void  )
inline
unsigned int cmnRandomSequence::ExtractRandomUnsignedInt ( const unsigned int  min,
const unsigned int  max 
)
inline
void cmnRandomSequence::ExtractRandomUnsignedIntArray ( const unsigned int  min,
const unsigned int  max,
unsigned int *  array,
const size_t  arraySize 
)
inline
unsigned long cmnRandomSequence::ExtractRandomUnsignedLong ( void  )
inline
unsigned long cmnRandomSequence::ExtractRandomUnsignedLong ( const unsigned long  min,
const unsigned long  max 
)
inline
void cmnRandomSequence::ExtractRandomUnsignedLongArray ( const unsigned long  min,
const unsigned long  max,
unsigned long *  array,
const size_t  arraySize 
)
inline
unsigned long long cmnRandomSequence::ExtractRandomUnsignedLongLong ( void  )
inline
unsigned long long cmnRandomSequence::ExtractRandomUnsignedLongLong ( const unsigned long long  min,
const unsigned long long  max 
)
inline
void cmnRandomSequence::ExtractRandomUnsignedLongLongArray ( const unsigned long long  min,
const unsigned long long  max,
unsigned long long *  array,
const size_t  arraySize 
)
inline
unsigned short cmnRandomSequence::ExtractRandomUnsignedShort ( void  )
inline
unsigned short cmnRandomSequence::ExtractRandomUnsignedShort ( const unsigned short  min,
const unsigned short  max 
)
inline
void cmnRandomSequence::ExtractRandomUnsignedShortArray ( const unsigned short  min,
const unsigned short  max,
unsigned short *  array,
const size_t  arraySize 
)
inline
template<typename _valueType >
void cmnRandomSequence::ExtractRandomValue ( _valueType &  result)

Extract a random value for a templated type. These methods are specialized by the type argument so that they can be used generically, e.g., in sequence testing.

template<typename _valueType >
void cmnRandomSequence::ExtractRandomValue ( const _valueType  min,
const _valueType  max,
_valueType &  result 
)
inline
template<typename _valueType >
CISST_DEFINE_TEMPLATE_FUNCTION_SPECIALIZATION void cmnRandomSequence::ExtractRandomValueArray ( const _valueType  min,
const _valueType  max,
_valueType *  array,
const size_t  arraySize 
)
inline
static cmnRandomSequence& cmnRandomSequence::GetInstance ( void  )
inlinestatic

Access the Singleton instance

SeedType cmnRandomSequence::GetSeed ( void  ) const
inline

Return the randomization seed for this object

SequenceCounterType cmnRandomSequence::GetSequencePosition ( void  ) const
inline

Return the number of random sequence elements extracted so far.

cmnRandomSequence& cmnRandomSequence::operator= ( const cmnRandomSequence other)
inlineprotected

Assignment operator – see copy constructor

void cmnRandomSequence::SetSeed ( const SeedType  seed)
inline

Set the randomization seed to the given value, and move to sequence position 0.

void cmnRandomSequence::SetSequencePosition ( const SequenceCounterType  position)
inline

Move to the specified position (zero based) in the random sequence starting with the current seed.

Member Data Documentation

const SeedType cmnRandomSequence::DefaultSeed
static

The default randomization seed.

const ElementaryRandomNumber cmnRandomSequence::LowerRandomBound
static

the lower bound for a value of an elementary random number

cmnRandomSequence cmnRandomSequence::RandomInstance
staticprotected

The Singleton instance

const ElementaryRandomNumber cmnRandomSequence::UpperRandomBound
static

the upper bound for a value of an elementary random number


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