#include <SgRandom.h>
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 27 of file SgRandom.h.
Public Member Functions | |
SgRandom () | |
~SgRandom () | |
unsigned int | Int () |
Get a random integer. | |
int | Int (int range) |
Get a random integer in [0. | |
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 SgRandom & | Global () |
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 GlobalData & | GetGlobalData () |
Return global data. | |
Private Attributes | |
boost::mt19937 | m_generator |
Classes | |
struct | GlobalData |
SgRandom::SgRandom | ( | ) |
Definition at line 25 of file SgRandom.cpp.
References GetGlobalData(), SgRandom::GlobalData::m_allGenerators, and SetSeed().
SgRandom::~SgRandom | ( | ) |
Definition at line 31 of file SgRandom.cpp.
References GetGlobalData(), and SgRandom::GlobalData::m_allGenerators.
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 42 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.
Definition at line 36 of file SgRandom.cpp.
Referenced by SgEvaluatedMoves::BestMove(), SgSortedMoves< MOVE, VALUE, SIZE >::DeleteEqual(), SgHash< N >::Random(), and SgSortedMoves< MOVE, VALUE, SIZE >::ShuffleInterval().
int SgRandom::Int | ( | int | range | ) |
Get a random integer in [0.
.range - 1].
Definition at line 111 of file SgRandom.h.
References Int(), Max(), SG_ASSERT, and SG_ASSERTRANGE.
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 106 of file SgRandom.h.
References m_generator.
Referenced by SgEvaluatedMoves::BestMove(), Int(), SgHash< N >::Random(), RandomEvent(), and Range().
unsigned int SgRandom::Max | ( | ) |
Maximum value.
Definition at line 120 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 125 of file SgRandom.h.
References m_generator.
bool SgRandom::RandomEvent | ( | unsigned int | threshold | ) |
return true if random number SgRandom() <= threshold
Definition at line 130 of file SgRandom.h.
References Int().
int SgRandom::Range | ( | int | min, | |
int | max | |||
) |
Get a random integer in [min, max - 1].
Definition at line 135 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 48 of file SgRandom.cpp.
References GetGlobalData(), and SgRandom::GlobalData::m_seed.
Referenced by SgGtpCommands::CmdGetRandomSeed().
void SgRandom::SetSeed | ( | ) | [private] |
Definition at line 53 of file SgRandom.cpp.
References GetGlobalData(), m_generator, and SgRandom::GlobalData::m_seed.
Referenced by SgGtpCommands::CmdSetRandomSeed(), SetSeed(), and SgRandom().
void SgRandom::SetSeed | ( | int | seed | ) | [static] |
Set random seed for all existing and future instances of SgRandom.
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() |
Definition at line 61 of file SgRandom.cpp.
References GetGlobalData(), SgRandom::GlobalData::m_allGenerators, SgRandom::GlobalData::m_seed, SetSeed(), and SgDebug().
boost::mt19937 SgRandom::m_generator [private] |
Definition at line 101 of file SgRandom.h.
Referenced by Int(), Max(), PercentageThreshold(), and SetSeed().