< index
< 9. Pseudorandom number generator
< 9.1 Creating a generator

=====================================
9.2 Using a generator
=====================================

> 9.3 Destroying a generator

TCODRandom::getInt

Once you obtained a generator (using one of those methods), you can get random numbers using following functions :
To get a random integer :

C++ : int TCODRandom::getInt(int min, int max)
C   : int TCOD_random_get_int(TCOD_random_t mersenne, int min, int max)
Py  : random_get_int(mersenne, mi, ma)

ParameterDescription
mersenneIn the C version, the generator handler, returned by the initialization functions. If NULL, the default generator is used..
min, maxRange of values returned. Each time you call this function, you get a number between (including) min and max

TCODRandom::getFloat

To get a random floating point number :

C++ : float TCODRandom::getFloat(float min, float max)
C   : float TCOD_random_get_float(TCOD_random_t mersenne, float min, float max)
Py  : random_get_float(mersenne, mi, ma)

ParameterDescription
mersenneIn the C version, the generator handler, returned by the initialization functions. If NULL, the default generator is used.
min, maxRange of values returned. Each time you call this function, you get a number between (including) min and max

Example :

C++ : // default generator
      TCODRandom * default = TCODRandom::getInstance();
      int aRandomIntBetween0And1000 = default->getInt(0,1000);
      // another random generator
      TCODRandom *myRandom = new TCODRandom();
      float aRandomFloatBetween0And1000 = myRandom->getFloat(0.0f,1000.0f);
C   : /* 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);
Py  : # 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)

TCODRandom::getGaussian*

To get a random number, either integer or floating point, with an approximated Gaussian distribution:

C++ : float TCODRandom::getGaussianFloat(float min, float max)
      int TCODRandom::getGaussianInt(int min, int max)
C   : float TCOD_random_get_gaussian_float(TCOD_random_t mersenne, float min, float max)
      int TCOD_random_get_gaussian_int(TCOD_random_t mersenne, int min, int max)
Py  : random_get_gaussian_float(mersenne, mi, ma)
      random_get_gaussian_int(mersenne, mi, ma)

ParameterDescription
mersenneIn the C version, the generator handler, returned by the initialization functions. If NULL, the default generator is used.
min, maxRange of values returned. Each time you call this function, you get a number between (including) min and max.
Due to the Gaussian distribution, most values are near (min+max)/2
The integer version of the function will work best at larger deltas (max-min).

TCODRandom::save

You can save the state of a generator with :

C++ : TCODRandom *TCODRandom::save() const
C   : TCOD_random_t TCOD_random_save(TCOD_random_t mersenne)
Py  : random_save(mersenne)

ParameterDescription
mersenneIn the C version, the generator handler, returned by the initialization functions. If NULL, the default generator is used.

TCODRandom::restore

And restore it later. This makes it possible to get the same serie of number several times with a single generator.

C++ : void TCODRandom::restore(const TCODRandom *backup)
C   : void TCOD_random_restore(TCOD_random_t mersenne, TCOD_random_t backup)
Py  : random_restore(mersenne, backup)

ParameterDescription
mersenneIn the C version, the generator handler, returned by the initialization functions. If NULL, the default generator is used.
Example :

C++ : // 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);
      // => number1 == number2
C   : /* 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);
      /* number1 == number2 */
Py  : # 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)
      # number1 == number2