< index < 9. Pseudorandom number generator | ===================================== 9.1 Creating a generator ===================================== | > 9.2 Using a generator

### TCODRandom::getInstance

The simplest way to get random number is to use the default generator. The first time you get this generator, it is initialized by calling TCOD_random_new. Then, on successive calls, this function returns the same generator (singleton pattern).

    C++ : static TCODRandom *TCODRandom::getInstance()
    C   : TCOD_random_t TCOD_random_get_instance()
    Py  : random_get_instance()

### TCODRandom::TCODRandom

You can also create as many generators as you want with a random seed (the number of seconds since Jan 1 1970 at the time the constructor is called). Warning ! If you call this function several times in the same second, it will return the same generator.

    C++ : TCODRandom::TCODRandom(TCOD_random_algo_t algo = TCOD_RNG_CMWC)
    C   : TCOD_random_t TCOD_random_new(TCOD_random_algo_t algo)
    Py  : random_new(algo=RNG_CMWC)

Parameter | Description
--- | ---
algo | The PRNG algorithm the generator should be using. Possible values are: * TCOD_RNG_MT for Mersenne Twister, * TCOD_RNG_CMWC for Complementary Multiply-With-Carry.

Finally, you can create generators with a specific seed. Those allow you to get a reproducible set of random numbers. You can for example save a dungeon in a file by saving only the seed used for its generation (provided you have a determinist generation algorithm)

    C++ : TCODRandom::TCODRandom(TCOD_random_algo_t algo = TCOD_RNG_CMWC, uint32 seed);
    C   : TCOD_random_t TCOD_random_new_from_seed(TCOD_random_algo_t algo, uint32 seed);
    Py  : random_new_from_seed(seed, algo=RNG_CMWC)

Parameter | Description
--- | ---
seed | The 32 bits seed used to initialize the generator. Two generators created with the same seed will generate the same set of pseudorandom numbers.
algo | The PRNG algorithm the generator should be using.

Example :

    C++ : // default generator
          TCODRandom * default = TCODRandom::getInstance();
          // another random generator
          TCODRandom *myRandom = new TCODRandom();
          // a random generator with a specific seed
          TCODRandom *myDeterministRandom = new TCODRandom(0xdeadbeef);
    C   : /* default generator */
          TCOD_random_t default = TCOD_random_get_instance();
          /* another random generator */
          TCOD_random_t my_random = TCOD_random_new(TCOD_RNG_CMWC);
          /* a random generator with a specific seed */
          TCOD_random_t my_determinist_random = TCOD_random_new_from_seed(TCOD_RNG_CMWC,0xdeadbeef);
    Py  : # default generator
          default = libtcod.random_get_instance()
          # another random generator
          my_random = libtcod.random_new()
          # a random generator with a specific seed
          my_determinist_random = libtcod.random_new_from_seed(0xdeadbeef)