new WeightedRandom(…options)
A set of values with weights, which can return a random value with probability corresponding to its weight.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
options |
Array |
<repeatable> |
Any number of rrays of length 2, each consisting of an arbitrary value and the weight assigned to it. Alternatively, the constructor may be called with a single object where the keys are options and the assosciated values are their weights. |
- Source:
Methods
choose()
Returns a random option. The chance of a given option being returned
is equal to (that option's weight) / (total of all options' weights).
E.g. if the constructor was passed ['a', 1], ['b', 2]
, there is a
1/3 chance of 'a'
being returned and a 2/3 chance of 'b'
being returned.
- Source: