Random number generator. More...
#include <SgRandom.h>
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 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 |
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.
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.
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.
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.
range | The upper limit of the interval (exclusive) |
[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.
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] |
Definition at line 50 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 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 | ) |
Definition at line 167 of file SgRandom.h.
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.
range | The upper limit of the interval (exclusive) |
[0..range - 1]
Definition at line 158 of file SgRandom.h.
References Int(), SG_ASSERT, and SG_ASSERTRANGE.
boost::mt19937 SgRandom::m_generator [private] |
Definition at line 111 of file SgRandom.h.
Referenced by Int(), Max(), PercentageThreshold(), and SetSeed().