Math Weighted_random
scripts/vscripts/alyxlib/math/weighted_random.lua
Methods
Add
Add 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 used.
Parameters
tbl
table
Table of values that will be returned.weight(optional)
number
Weight for this table.
TotalWeight
Get the sum of all weights in the WeightedRandom.
Returns
- number
The sum of all weights.
Random
Pick a random table from the list of weighted tables.
Returns
- WeightedRandomItem|table
The chosen table.
Functions
WeightedRandom
Create a new WeightedRandom object with given weights.
E.g.
Example
Params:
Parameters
weights
WeightedRandomItem[]
List of weighted tables.
Returns
- WeightedRandom
WeightedRandom object.
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. |