Math Weighted_random
scripts/vscripts/alyxlib/math/weighted_random.lua
Methods
Add
Adds a table value with an associated weight.
If tbl already has a weight key then weight parameter can be omitted.
Note: The table tbl is not cloned, the given reference is inserted into the list.
Parameters
tbl
table
Table of values that will be returnedweight(optional)
number
Weight for this table
TotalWeight
Gets the sum of all weights in the WeightedRandom.
Returns
- number
The sum of all weights
Random
Picks a random table from the list of weighted tables.
Returns
- WeightedRandomItem|table
The randomly selected table
Functions
WeightedRandom
Creates a new WeightedRandom instance with given weights.
E.g.
Example
Parameters
weights
WeightedRandomItem[]
List of weighted tables
Returns
- WeightedRandom
The new WeightedRandom instance
Types
WeightedRandom
A list of tables with associated weights.
WeightedRandomItem
Individual item in the list of weighted tables.
| Field | Type | Description |
|---|---|---|
| weight | number |
The weight of this item. |