Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  
Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes

SgRandom Class Reference

Random number generator. More...

#include <SgRandom.h>

List of all members.

Classes

struct  GlobalData

Public Member Functions

 SgRandom ()
 ~SgRandom ()
unsigned int Int ()
 Get a random integer.
int Int (int range)
 Get a random integer in an interval.
std::size_t Int (std::size_t range)
 See SgRandom::Int(int).
int SmallInt (int range)
 Get a small random integer in an interval.
std::size_t SmallInt (std::size_t range)
 See SgRandom::SmallInt(int).
int Range (int min, int max)
 Get a random integer in [min, max - 1].
unsigned int Max ()
 Maximum value.
unsigned int PercentageThreshold (int percentage)
 convert percentage between 0 and 99 to a threshold for RandomEvent.
bool RandomEvent (unsigned int threshold)
 return true if random number SgRandom() <= threshold

Static Public Member Functions

static SgRandomGlobal ()
 Return the global random number generator.
static void SetSeed (int seed)
 Set random seed for all existing and future instances of SgRandom.
static int Seed ()
 Get random seed.

Private Member Functions

void SetSeed ()

Static Private Member Functions

static GlobalDataGetGlobalData ()
 Return global data.

Private Attributes

boost::mt19937 m_generator

Detailed Description

Random number generator.

Uses a Mersenne Twister, because this is faster than std::rand() and game playing programs usually need faster random numbers more than high quality ones. All random generators are internally registered to make it possible to change the random seed for all of them.

SgRandom is thread-safe (w.r.t. different instances) after construction (the constructor is not thread-safe, because it uses a global variable for registration).

Definition at line 25 of file SgRandom.h.


Constructor & Destructor Documentation

SgRandom::SgRandom (  ) 

Definition at line 22 of file SgRandom.cpp.

References GetGlobalData(), SgRandom::GlobalData::m_allGenerators, and SetSeed().

SgRandom::~SgRandom (  ) 

Definition at line 28 of file SgRandom.cpp.

References GetGlobalData(), and SgRandom::GlobalData::m_allGenerators.


Member Function Documentation

SgRandom::GlobalData & SgRandom::GetGlobalData (  )  [static, private]

Return global data.

Global data is stored as a static variable in this function to ensure that it is initialized at first call if SgRandom is used in global variables of other compilation units.

Definition at line 39 of file SgRandom.cpp.

Referenced by Seed(), SetSeed(), SgRandom(), and ~SgRandom().

SgRandom & SgRandom::Global (  )  [static]

Return the global random number generator.

The global generator is stored as a static variable in this function to ensure that it is initialized at first call if SgRandom is used in global variables of other compilation units.

Note:
The global random number generator is not thread-safe.

Definition at line 33 of file SgRandom.cpp.

Referenced by SgEvaluatedMoves::BestMove(), SgSortedMoves< MOVE, VALUE, SIZE >::DeleteEqual(), SgHash< N >::Random(), and SgSortedMoves< MOVE, VALUE, SIZE >::ShuffleInterval().

unsigned int SgRandom::Int (  ) 

Get a random integer.

Uses a fast random generator (the Mersenne Twister boost::mt19937), because in games and Monte Carlo simulations, speed is more important than quality.

Definition at line 116 of file SgRandom.h.

References m_generator.

Referenced by SgEvaluatedMoves::BestMove(), Int(), SgHash< N >::Random(), RandomEvent(), Range(), SgSortedMoves< MOVE, VALUE, SIZE >::ShuffleInterval(), and SmallInt().

int SgRandom::Int ( int  range  ) 

Get a random integer in an interval.

Parameters:
range The upper limit of the interval (exclusive)
Precondition:
range > 0
range <= SgRandom::Max()
Returns:
An integer in [0..range - 1]

Definition at line 121 of file SgRandom.h.

References Int(), Max(), SG_ASSERT, and SG_ASSERTRANGE.

std::size_t SgRandom::Int ( std::size_t  range  ) 

See SgRandom::Int(int).

Definition at line 130 of file SgRandom.h.

References Int(), Max(), and SG_ASSERT.

unsigned int SgRandom::Max (  ) 

Maximum value.

Definition at line 138 of file SgRandom.h.

References m_generator.

Referenced by Int().

unsigned int SgRandom::PercentageThreshold ( int  percentage  ) 

convert percentage between 0 and 99 to a threshold for RandomEvent.

Use as in following example: const unsigned int percent80 = PercentageThreshold(80);

Definition at line 143 of file SgRandom.h.

References m_generator.

bool SgRandom::RandomEvent ( unsigned int  threshold  ) 

return true if random number SgRandom() <= threshold

Definition at line 148 of file SgRandom.h.

References Int().

int SgRandom::Range ( int  min,
int  max 
)

Get a random integer in [min, max - 1].

Definition at line 153 of file SgRandom.h.

References Int().

int SgRandom::Seed (  )  [static]

Get random seed.

See SetSeed(int) for the special meaning of zero and negative values.

Definition at line 45 of file SgRandom.cpp.

References GetGlobalData(), and SgRandom::GlobalData::m_seed.

Referenced by SgGtpCommands::CmdGetRandomSeed().

void SgRandom::SetSeed (  )  [private]
void SgRandom::SetSeed ( int  seed  )  [static]

Set random seed for all existing and future instances of SgRandom.

Parameters:
seed The seed. If negative, no seed will be set. If zero, a non-deterministic random seed will be used (e.g. derived from the current time). Also calls std::srand()
Note:
This function is not thread-safe.

Definition at line 58 of file SgRandom.cpp.

References GetGlobalData(), SgRandom::GlobalData::m_allGenerators, SgRandom::GlobalData::m_seed, SetSeed(), and SgDebug().

std::size_t SgRandom::SmallInt ( std::size_t  range  ) 

See SgRandom::SmallInt(int).

Definition at line 167 of file SgRandom.h.

References Int(), and SG_ASSERT.

int SgRandom::SmallInt ( int  range  ) 

Get a small random integer in an interval.

Uses only the lower 16 bits. Faster than SgRandom::Int(int) because it avoids the expensive modulo operation.

Parameters:
range The upper limit of the interval (exclusive)
Precondition:
range > 0
range <= (1 << 16)
Returns:
An integer in [0..range - 1]

Definition at line 158 of file SgRandom.h.

References Int(), SG_ASSERT, and SG_ASSERTRANGE.


Member Data Documentation

boost::mt19937 SgRandom::m_generator [private]

Definition at line 111 of file SgRandom.h.

Referenced by Int(), Max(), PercentageThreshold(), and SetSeed().


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


Sun Mar 13 2011 Doxygen 1.7.1