Random module

Random numbers, drawn from the sequence the simulation itself runs on.

The savegame carries that sequence, so what a script draws comes back the same after a reload, and a script needs no seed of its own. The sequence moves on for the engine as well: a script drawing every frame changes what the creatures decide next.

Lua's own math.random is a separate generator that nothing saves. It has no place in anything the simulation reads.

Functions

  • trx.random.random()
    A fraction of one, the whole number itself excepted.

    Returns: number. A value in [0, 1).

  • trx.random.randint(a, b)
    A whole number between two bounds, both of them included.

    Parameters:

    • a (integer). Lowest value.
    • b (integer). Highest value. Below the lowest raises.

    Returns: integer. A value in [a, b].

    Example:

    local pips = trx.random.randint(1, 6)
    
  • trx.random.choice(seq)
    One item out of a list, each as likely as the next.

    Parameters:

    • seq (a list of any). What to choose from. An empty list raises.

    Returns: any. The item chosen.

    Example:

    local sample = trx.random.choice({
      trx.catalog.samples.LARA_NO,
      trx.catalog.samples.LARA_YES,
    })
    
  • trx.random.choices(seq, [weights], [k])
    Several items out of a list, drawn one after another so that the same item can come up more than once. Weights give some items a greater share than others.

    Parameters:

    • seq (a list of any). What to choose from. An empty list raises.
    • weights (a list of number, optional). One share per item, none of them negative and not all zero. Defaults to an equal share each.
    • k (integer, optional, default 1). How many to draw. Below 0 raises.

    Returns: a list of any. The items chosen.

    Example:

    local drops = trx.random.choices({ "medipack", "ammo" }, { 1, 3 }, 5)
    
  • trx.random.angle()
    A direction, anywhere around the turn.

    Returns: trx.math.Angle. An angle within one turn.

    Example:

    trx.lara.item.rot = { x = 0, y = trx.random.angle(), z = 0 }
    
  • trx.random.chance(p)
    Whether something with the given likelihood happens this time.

    Parameters:

    • p (number). How likely, from 0 for never to 1 for always.

    Returns: boolean. Whether it happens.

    Example:

    if trx.random.chance(0.25) then
      trx.sound.play(trx.catalog.samples.LARA_NO)
    end