CityRP Documentation Repository

WEIGHTED_RANDOM

Weighted random table calculators.

Functions

SharedWEIGHTED_RANDOM:Add(data, weight) → WEIGHTED_RANDOMSource ☍

Add a new entry to the weighted table.

Parameters

  • dataanyThe data to insert. If a table, the .weight field is used as the weight.
  • weightnumber?The manual override weight value, or nil to use the entry's .weight or default to 1.

Returns

  1. WEIGHTED_RANDOMThe weighted table (for chaining).
SharedWEIGHTED_RANDOM:New() → WEIGHTED_RANDOMSource ☍

Create a new weighted random table instance.

Returns

  1. WEIGHTED_RANDOMThe newly created instance.
SharedWEIGHTED_RANDOM:Random() → anySource ☍

Pull out a random value from the table, respecting the weights of the table.

Returns

  1. anyThe selected value, or nil if the table is empty.

Tables

SharedWEIGHTED_RANDOMSource ☍

A small helper for selecting elements at random where each element has an associated weight. Supports adding arbitrary data entries with optional weight overrides and sampling respecting weights.

Classes

SharedWEIGHTED_RANDOMSource ☍

A small helper for selecting elements at random where each element has an associated weight. Supports adding arbitrary data entries with optional weight overrides and sampling respecting weights.

Fields

SharedWEIGHTED_RANDOM.contentsSource ☍

Internal entries with weight and range.

SharedWEIGHTED_RANDOM.totalSource ☍

Total weight of all entries.

SharedWEIGHTED_RANDOM.totalSource ☍