Random module

Random numbers, drawn from one of the two sequences the engine runs on.

The module's own calls draw from the control stream. This is the sequence the simulation runs on, so a script that draws every frame changes what the creatures decide next. The draw stream, trx.random.draw, is the one the original game keeps for what is only seen. Drawing from it leaves the simulation as it was. Both streams are the same generator and offer the same calls, described in trx.random.Stream.

The savegame carries both sequences. A script's draws come back the same after a reload, and a script needs no seed of its own.

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

Properties

  • trx.random.control (trx.random.Stream). The sequence the simulation runs on, which the module's own functions draw from. (read-only)
  • trx.random.draw (trx.random.Stream). The sequence kept for what is only seen. Drawing from it leaves what the creatures decide next as it was, which is what the original game keeps it for. (read-only)

Structures

  • trx.random.Stream

    One of the engine's two random sequences.

    Drawing from trx.random.control changes what the game does next, because the simulation runs on it. Drawing from trx.random.draw changes nothing, because only the picture uses it.

    Both have the same calls. The module's own functions draw from the control stream.

    Methods:

    • stream:angle()
      A direction, anywhere around the turn.

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

    • stream: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.

    • stream: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.

    • stream: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.

    • stream: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.draw:randint(1, 6)
      
    • stream:random()
      A fraction of one, the whole number itself excepted.

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

    • stream:randrange(n)
      A whole number below a bound, counted from zero. The bound itself never comes up.

      Parameters:

      • n (integer). How many values there are. Below 1 raises.

      Returns: integer. A value in [0, n).

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.randrange(n)
    A whole number below a bound, counted from zero. The bound itself never comes up.

    Parameters:

    • n (integer). How many values there are. Below 1 raises.

    Returns: integer. A value in [0, n).

    Example:

    local side = trx.random.randrange(6) + 1
    
  • 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