NCBI C++ ToolKit
Classes | Typedefs | Enumerations | Enumerator | Functions | Variables
Random Number Generation
+ Collaboration diagram for Random Number Generation:

Classes

class  CRandom
 CRandom:: More...
 
class  CRandomException
 Exceptions generated by CRandom class. More...
 

Typedefs

typedef Uint4 CRandom::TValue
 Type of the generated integer value and/or the seed value. More...
 

Enumerations

enum  CRandom::EGetRandMethod { CRandom::eGetRand_LFG , CRandom::eGetRand_Sys }
 Random generator to use in the GetRand() functions. More...
 
enum  { CRandom::kStateSize = 33 }
 
enum  CRandomException::EErrCode { CRandomException::eUnavailable , CRandomException::eUnexpectedRandMethod , CRandomException::eSysGeneratorError }
 

Functions

 CRandom::CRandom (EGetRandMethod method=eGetRand_LFG)
 If "method" is: More...
 
 CRandom::CRandom (TValue seed)
 Use LFG random generator seeded with "seed". More...
 
TValue CRandom::GetRand (void)
 Get the next random number in the interval [0..GetMax()] (inclusive) More...
 
TValue CRandom::GetRand (TValue min_value, TValue max_value)
 Get random number in the interval [min_value..max_value] (inclusive) More...
 
Uint8 CRandom::GetRandUint8 (void)
 Get random Uint8 number. More...
 
Uint8 CRandom::GetRandUint8 (Uint8 min_value, Uint8 max_value)
 Get random number in the interval [min_value..max_value] (inclusive) More...
 
size_t CRandom::GetRandSize_t (size_t min_value, size_t max_value)
 Get random number in the interval [min_value..max_value] (inclusive) More...
 
TValue CRandom::GetRandIndex (TValue size)
 Get random number in the interval [0..size-1] (e.g. More...
 
Uint8 CRandom::GetRandIndexUint8 (Uint8 size)
 Get random number in the interval [0..size-1] (e.g. More...
 
size_t CRandom::GetRandIndexSize_t (size_t size)
 Get random number in the interval [0..size-1] (e.g. More...
 
static TValue CRandom::GetMax (void)
 The max. value GetRand() returns. More...
 
EGetRandMethod CRandom::GetRandMethod (void) const
 Get the random generator type. More...
 
void CRandom::Randomize (void)
 Re-initialize (re-seed) the generator using platform-specific randomization. More...
 
void CRandom::SetSeed (TValue seed)
 Seed the random number generator with "seed". More...
 
TValue CRandom::GetSeed (void) const
 Get the last set seed (LFG only) More...
 
void CRandom::Reset (void)
 Reset random number generator to initial startup condition (LFG only) More...
 
TValue CRandom::x_GetRand32Bits (void)
 
Uint8 CRandom::x_GetRand64Bits (void)
 
TValue CRandom::x_GetSysRand32Bits (void) const
 
 CRandom::CRandom (const CRandom &)
 
CRandomCRandom::operator= (const CRandom &)
 
virtual const char * CRandomException::GetErrCodeString (void) const override
 Get error code interpreted as text. More...
 
 CRandomException::NCBI_EXCEPTION_DEFAULT (CRandomException, CException)
 

Variables

EGetRandMethod CRandom::m_RandMethod
 
TValue CRandom::m_State [kStateSize]
 
int CRandom::m_RJ
 
int CRandom::m_RK
 
TValue CRandom::m_Seed
 

Detailed Description

Typedef Documentation

◆ TValue

Type of the generated integer value and/or the seed value.

Definition at line 69 of file random_gen.hpp.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
private
Enumerator
kStateSize 

Definition at line 159 of file random_gen.hpp.

◆ EErrCode

Enumerator
eUnavailable 

System-dependent generator is not available.

eUnexpectedRandMethod 

The user called method which is not allowed for the used generator.

eSysGeneratorError 

Error getting a random value from the system-dependent generator.

Definition at line 182 of file random_gen.hpp.

◆ EGetRandMethod

Random generator to use in the GetRand() functions.

Enumerator
eGetRand_LFG 

Use lagged Fibonacci (LFG) random number generator.

eGetRand_Sys 

Use system-dependent random generator.

Definition at line 72 of file random_gen.hpp.

Function Documentation

◆ CRandom() [1/3]

CRandom::CRandom ( const CRandom )
private

◆ CRandom() [2/3]

CRandom::CRandom ( EGetRandMethod  method = eGetRand_LFG)

If "method" is:

  • eGetRand_LFG – use LFG generator seeded with a hard-coded seed
  • eGetRand_Sys – use system-dependent generator

Definition at line 215 of file random_gen.cpp.

References CRandom::eGetRand_Sys, NCBI_THROW, CRandom::Reset(), and s_RandomSupplier.

◆ CRandom() [3/3]

CRandom::CRandom ( TValue  seed)

Use LFG random generator seeded with "seed".

Definition at line 227 of file random_gen.cpp.

References seed, and CRandom::SetSeed().

◆ GetErrCodeString()

virtual const char* CRandomException::GetErrCodeString ( void  ) const
inlineoverridevirtual

◆ GetMax()

CRandom::TValue CRandom::GetMax ( void  )
inlinestatic

The max. value GetRand() returns.

Definition at line 295 of file random_gen.hpp.

Referenced by CNCDistributionConf::GenerateBlobKey(), and alp_data::ran2().

◆ GetRand() [1/2]

CRandom::TValue CRandom::GetRand ( TValue  min_value,
TValue  max_value 
)
inline

Get random number in the interval [min_value..max_value] (inclusive)

See also
EGetRandMethod
Note
eGetRand_LFG generator could be 100 times faster than eGetRand_Sys one

Definition at line 277 of file random_gen.hpp.

References CRandom::GetRandIndex(), and min_value.

◆ GetRand() [2/2]

CRandom::TValue CRandom::GetRand ( void  )
inline

◆ GetRandIndex()

CRandom::TValue CRandom::GetRandIndex ( TValue  size)
inline

Get random number in the interval [0..size-1] (e.g.

index in array)

See also
EGetRandMethod
Note
eGetRand_LFG generator could be 100 times faster than eGetRand_Sys one

Definition at line 251 of file random_gen.hpp.

References r(), ncbi::grid::netcache::search::fields::size, and CRandom::x_GetRand32Bits().

Referenced by CRandom::GetRand(), CRandom::GetRandIndexSize_t(), and CRandom::GetRandIndexUint8().

◆ GetRandIndexSize_t()

size_t CRandom::GetRandIndexSize_t ( size_t  size)
inline

Get random number in the interval [0..size-1] (e.g.

index in array)

See also
EGetRandMethod
Note
eGetRand_LFG generator could be 100 times faster than eGetRand_Sys one

Definition at line 267 of file random_gen.hpp.

References CRandom::GetRandIndex(), CRandom::GetRandIndexUint8(), and ncbi::grid::netcache::search::fields::size.

Referenced by CRandom::GetRandSize_t().

◆ GetRandIndexUint8()

Uint8 CRandom::GetRandIndexUint8 ( Uint8  size)

Get random number in the interval [0..size-1] (e.g.

index in array)

See also
EGetRandMethod
Note
eGetRand_LFG generator could be 100 times faster than eGetRand_Sys one

Definition at line 331 of file random_gen.cpp.

References CRandom::GetRandIndex(), CRandom::GetRandUint8(), kMax_UI4, r(), and ncbi::grid::netcache::search::fields::size.

Referenced by CRandom::GetRandIndexSize_t(), and CRandom::GetRandUint8().

◆ GetRandMethod()

CRandom::EGetRandMethod CRandom::GetRandMethod ( void  ) const
inline

Get the random generator type.

Definition at line 301 of file random_gen.hpp.

References CRandom::m_RandMethod.

◆ GetRandSize_t()

size_t CRandom::GetRandSize_t ( size_t  min_value,
size_t  max_value 
)
inline

Get random number in the interval [min_value..max_value] (inclusive)

See also
EGetRandMethod
Note
eGetRand_LFG generator could be 100 times faster than eGetRand_Sys one

Definition at line 289 of file random_gen.hpp.

References CRandom::GetRandIndexSize_t(), and min_value.

◆ GetRandUint8() [1/2]

Uint8 CRandom::GetRandUint8 ( Uint8  min_value,
Uint8  max_value 
)
inline

Get random number in the interval [min_value..max_value] (inclusive)

See also
EGetRandMethod
Note
eGetRand_LFG generator could be 100 times faster than eGetRand_Sys one

Definition at line 283 of file random_gen.hpp.

References CRandom::GetRandIndexUint8(), and min_value.

◆ GetRandUint8() [2/2]

Uint8 CRandom::GetRandUint8 ( void  )
inline

Get random Uint8 number.

See also
EGetRandMethod
Note
eGetRand_LFG generator could be 100 times faster than eGetRand_Sys one

Definition at line 244 of file random_gen.hpp.

References CRandom::x_GetRand32Bits().

Referenced by NDirectNetStorageImpl::SContext::Create(), CRandom::GetRandIndexUint8(), and CPubseqGatewayApp::Run().

◆ GetSeed()

CRandom::TValue CRandom::GetSeed ( void  ) const

Get the last set seed (LFG only)

Attention
Throw exception if non-LFG (i.e. system) generator is used.

Definition at line 312 of file random_gen.cpp.

References CRandom::eGetRand_Sys, CRandom::m_RandMethod, CRandom::m_Seed, and NCBI_THROW.

◆ NCBI_EXCEPTION_DEFAULT()

CRandomException::NCBI_EXCEPTION_DEFAULT ( CRandomException  ,
CException   
)

◆ operator=()

CRandom& CRandom::operator= ( const CRandom )
private

◆ Randomize()

void CRandom::Randomize ( void  )

Re-initialize (re-seed) the generator using platform-specific randomization.

Note
Does nothing if system generator is used.

Definition at line 267 of file random_gen.cpp.

References CTime::eCurrent, CRandom::eGetRand_Sys, CCurrentProcess::GetPid(), CThread::GetSelf(), CRandom::m_RandMethod, CTime::NanoSecond(), s_RandomSupplier, CTime::Second(), seed, and CRandom::SetSeed().

Referenced by CNcbiEncryptApp::GenerateKey(), and NDirectNetStorageImpl::SContext::Init().

◆ Reset()

void CRandom::Reset ( void  )

Reset random number generator to initial startup condition (LFG only)

Attention
Throw exception if non-LFG (i.e. system) generator is used.

Definition at line 234 of file random_gen.cpp.

References _ASSERT, CRandom::eGetRand_Sys, i, kStateOffset, CRandom::kStateSize, CRandom::m_RandMethod, CRandom::m_RJ, CRandom::m_RK, CRandom::m_State, and NCBI_THROW.

Referenced by CSubjectRangesSet::AddRange(), CRandom::CRandom(), and CSeqDBVol::SetOffsetRanges().

◆ SetSeed()

void CRandom::SetSeed ( TValue  seed)

◆ x_GetRand32Bits()

CRandom::TValue CRandom::x_GetRand32Bits ( void  )
inlineprivate

◆ x_GetRand64Bits()

Uint8 CRandom::x_GetRand64Bits ( void  )
private

◆ x_GetSysRand32Bits()

CRandom::TValue CRandom::x_GetSysRand32Bits ( void  ) const
private

Definition at line 323 of file random_gen.cpp.

References r(), and s_RandomSupplier.

Referenced by CRandom::x_GetRand32Bits().

Variable Documentation

◆ m_RandMethod

EGetRandMethod CRandom::m_RandMethod
private

◆ m_RJ

int CRandom::m_RJ
private

Definition at line 164 of file random_gen.hpp.

Referenced by CRandom::Reset(), CRandom::SetSeed(), and CRandom::x_GetRand32Bits().

◆ m_RK

int CRandom::m_RK
private

Definition at line 165 of file random_gen.hpp.

Referenced by CRandom::Reset(), CRandom::SetSeed(), and CRandom::x_GetRand32Bits().

◆ m_Seed

TValue CRandom::m_Seed
private

Definition at line 166 of file random_gen.hpp.

Referenced by CRandom::GetSeed(), and CRandom::SetSeed().

◆ m_State

TValue CRandom::m_State[kStateSize]
private

Definition at line 163 of file random_gen.hpp.

Referenced by CRandom::Reset(), CRandom::SetSeed(), and CRandom::x_GetRand32Bits().

Modified on Fri Sep 20 14:58:15 2024 by modify_doxy.py rev. 669887