appkit  1.5.1
RoadNarrows Robotics Application Kit
rnr::Random Class Reference

Random variable generators class. More...

#include <Random.h>

Public Member Functions

 Random (ulong_t luSeed=AUTO_SEED)
 Default initialization constructor. More...
 
virtual ~Random ()
 Destructor.
 
int randint ()
 Generates a random integer uniformally distrubuted between [-2^31, 2^32-1]. More...
 
int randrange (int nMin=0, int nMax=INT_MAX)
 Generates a random integer uniformally distrubuted between [nMin, nMax]. More...
 
s64_t randrange (s64_t nMin=0, s64_t nMax=LLONG_MAX)
 Generates a random 64-bit integer uniformally distrubuted between [nMin, nMax]. More...
 
float uniform (float fMin=0.0, float fMax=1.0)
 Generates a random float uniformally distrubuted between [fMin, fMax]. More...
 

Static Public Attributes

static const ulong_t AUTO_SEED = 0
 

Protected Attributes

struct drand48_data m_randState
 random generator opaque state.
 

Detailed Description

Random variable generators class.

Definition at line 79 of file Random.h.

Constructor & Destructor Documentation

Random::Random ( ulong_t  luSeed = AUTO_SEED)

Default initialization constructor.

Parameters
luSeedRandom generator seed.

Definition at line 72 of file Random.cxx.

References m_randState.

73 {
74  ushort_t seed[3]; // 48-bits
75 
76  if( luSeed == AUTO_SEED )
77  {
78  luSeed = (ulong_t)time(NULL);
79  }
80 
81  seed[0] = (ushort_t)(luSeed & 0xffff);
82  seed[1] = (ushort_t)((luSeed >> 16) & 0xffff);
83  seed[2] = (ushort_t)(seed[0] << 8) | (ushort_t)(seed[1] >> 8);
84 
85  seed48_r(seed, &m_randState);
86 }
struct drand48_data m_randState
random generator opaque state.
Definition: Random.h:145

Member Function Documentation

int rnr::Random::randint ( )
inline

Generates a random integer uniformally distrubuted between [-2^31, 2^32-1].

Returns
Random value.

Definition at line 102 of file Random.h.

References m_randState, randrange(), and uniform().

103  {
104  long lRandVal;
105 
106  mrand48_r(&m_randState, &lRandVal);
107 
108  return (int)lRandVal;
109  }
struct drand48_data m_randState
random generator opaque state.
Definition: Random.h:145
int Random::randrange ( int  nMin = 0,
int  nMax = INT_MAX 
)

Generates a random integer uniformally distrubuted between [nMin, nMax].

Parameters
nMinMinimum value.
nMaxMaximum value.
Returns
Random value.

Definition at line 88 of file Random.cxx.

References m_randState.

Referenced by randint(), and rnr::State::receiveEvent().

89 {
90  double lfRandVal;
91 
92  drand48_r(&m_randState, &lfRandVal);
93 
94  return (int)((double)(nMax - nMin) * lfRandVal + nMin);
95 }
struct drand48_data m_randState
random generator opaque state.
Definition: Random.h:145
s64_t Random::randrange ( s64_t  nMin = 0,
s64_t  nMax = LLONG_MAX 
)

Generates a random 64-bit integer uniformally distrubuted between [nMin, nMax].

Parameters
nMinMinimum value.
nMaxMaximum value.
Returns
Random value.

Definition at line 97 of file Random.cxx.

References m_randState.

98 {
99  double lfRandVal;
100 
101  drand48_r(&m_randState, &lfRandVal);
102 
103  return (s64_t)((double)(nMax - nMin) * lfRandVal + nMin);
104 }
struct drand48_data m_randState
random generator opaque state.
Definition: Random.h:145
float Random::uniform ( float  fMin = 0.0,
float  fMax = 1.0 
)

Generates a random float uniformally distrubuted between [fMin, fMax].

Parameters
fMinMinimum value.
fMaxMaximum value.
Returns
Random value.

Definition at line 106 of file Random.cxx.

References m_randState.

Referenced by UTThread::exec(), and randint().

107 {
108  double lfRandVal;
109 
110  drand48_r(&m_randState, &lfRandVal);
111 
112  return (float)((double)(fMax - fMin) * lfRandVal + fMin);
113 }
struct drand48_data m_randState
random generator opaque state.
Definition: Random.h:145

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