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

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

> 9.3 Getting a random int from a byte array
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

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)


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.

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 

insert a comment