7.3. Using a generator
Getting an integer
Once you obtained a generator (using one of those methods), you can get random numbers using the following functions, using either the explicit or simplified API where applicable:
//explicit API:
int TCODRandom::getInt(int min, int max, int mean = 0)
//simplified API:
int TCODRandom::get(int min, int max, int mean = 0)
int TCOD_random_get_int(TCOD_random_t mersenne, int min, int max)
int TCOD_random_get_int_mean(TCOD_random_t mersenne, int min, int max, int mean)
Parameter | Description |
---|---|
mersenne | In the C and Python versions, the generator handler, returned by the initialization functions. If NULL, the default generator is used.. |
min,max | Range of values returned. Each time you call this function, you get a number between (including) min and max |
mean | This is used to set a custom mean, ie, not min+((max-min)/2). It can even be outside of the min-max range. Using a mean will force the use of a weighted (Gaussian) distribution, even if linear is set. |
Getting a float
To get a random floating point number, using either the explicit or simplified API where applicable
//explicit API:
float TCODRandom::getFloat(float min, float max, float mean = 0.0f)
//simplified API:
float TCODRandom::get(float min, float max, float mean = 0.0f)
float TCOD_random_get_float(TCOD_random_t mersenne, float min, float max)
float TCOD_random_get_float_mean(TCOD_random_t mersenne, float min, float max, float mean)
random_get_float(mersenne, mi, ma)
float TCODRandom::getFloat(float min, float max)
Parameter | Description |
---|---|
mersenne | In the C and Python versions, the generator handler, returned by the initialization functions. If NULL, the default generator is used. |
min,max | Range of values returned. Each time you call this function, you get a number between (including) min and max |
mean | This is used to set a custom mean, ie, not min+((max-min)/2). It can even be outside of the min-max range. Using a mean will force the use of a weighted (Gaussian) distribution, even if linear is set. |
Example:
// default generator
TCODRandom * default = TCODRandom::getInstance();
int aRandomIntBetween0And1000 = default->getInt(0,1000);
int anotherRandomInt = default->get(0,1000);
// another random generator
TCODRandom *myRandom = new TCODRandom();
float aRandomFloatBetween0And1000 = myRandom->getFloat(0.0f,1000.0f);
float anotherRandomFloat = myRandom->get(0.0f,1000.0f);
// default generator
int a_random_int_between_0_and_1000 = TCOD_random_get_float(NULL,0,1000);
// another random generator
TCOD_random_t my_random = TCOD_random_new();
float a_random_float_between_0_and_1000 = TCOD_random_get_float(my_random,0.0f,1000.0f);
# default generator
a_random_int_between_0_and_1000 = libtcod.random_get_float(0,0,1000)
# another random generator
my_random = libtcod.random_new()
a_random_float_between_0_and_1000 = libtcod.random_get_float(my_random,0.0,1000.0)
Getting a double
To get a random double precision floating point number, using either the explicit or simplified API where applicable
//explicit API:
double TCODRandom::getDouble(double min, double max, double mean = 0.0f)
//simplified API:
double TCODRandom::get(double min, double max, double mean = 0.0f)
double TCOD_random_get_double(TCOD_random_t mersenne, double min, double max)
double TCOD_random_get_double_mean(TCOD_random_t mersenne, double min, double max, double mean)
Parameter | Description |
---|---|
mersenne | In the C and Python versions, the generator handler, returned by the initialization functions. If NULL, the default generator is used. |
min,max | Range of values returned. Each time you call this function, you get a number between (including) min and max |
mean | This is used to set a custom mean, ie, not min+((max-min)/2). It can even be outside of the min-max range. Using a mean will force the use of a weighted (Gaussian) distribution, even if linear is set. |
Example:
// default generator
TCODRandom * default = TCODRandom::getInstance();
int aRandomIntBetween0And1000 = default->getInt(0,1000);
int anotherRandomInt = default->get(0,1000);
// another random generator
TCODRandom *myRandom = new TCODRandom();
float aRandomFloatBetween0And1000 = myRandom->getFloat(0.0f,1000.0f);
float anotherRandomFloat = myRandom->get(0.0f,1000.0f);
// default generator
int a_random_int_between_0_and_1000 = TCOD_random_get_float(NULL,0,1000);
// another random generator
TCOD_random_t my_random = TCOD_random_new();
float a_random_float_between_0_and_1000 = TCOD_random_get_float(my_random,0.0f,1000.0f);
# default generator
a_random_int_between_0_and_1000 = libtcod.random_get_float(0,0,1000)
# another random generator
my_random = libtcod.random_new()
a_random_float_between_0_and_1000 = libtcod.random_get_float(my_random,0.0,1000.0)
Saving a RNG state
You can save the state of a generator with :
TCODRandom *TCODRandom::save() const
TCOD_random_t TCOD_random_save(TCOD_random_t mersenne)
random_save(mersenne)
TCODRandom TCODRandom::save()
Parameter | Description |
---|---|
mersenne | In the C and Python versions, the generator handler, returned by the initialization functions. If NULL, the default generator is used. |
Restoring a saved state
And restore it later. This makes it possible to get the same serie of number several times with a single generator.
void TCODRandom::restore(const TCODRandom *backup)
void TCOD_random_restore(TCOD_random_t mersenne, TCOD_random_t backup)
random_restore(mersenne, backup)
void TCODRandom::restore(TCODRandom backup)
Parameter | Description |
---|---|
mersenne | In the C and Python versions, the generator handler, returned by the initialization functions. If NULL, the default generator is used. |
Example:
// default generator
TCODRandom * default = TCODRandom::getInstance();
// save the state
TCODRandom *backup=default->save();
// get a random number (or several)
int number1 = default->getInt(0,1000);
// restore the state
default->restore(backup);
// get a random number
int number2 = default->getInt(0,1000);
// save default generator state
TCOD_random_t backup=TCOD_random_save(NULL);
// get a random number
int number1 = TCOD_random_get_float(NULL,0,1000);
// restore the state
TCOD_random_restore(NULL,backup);
// get a random number
int number2 = TCOD_random_get_float(NULL,0,1000);
# save default generator state
backup=libtcod.random_save(0)
# get a random number
number1 = libtcod.random_get_float(0,0,1000)
# restore the state
libtcod.random_restore(0,backup)
# get a random number
number2 = libtcod.random_get_float(0,0,1000)