Skip to content

Function: randomWeighted() ​

ts
function randomWeighted<T>(items): T;

Randomly picks a value by weight. 按权重随机选择值。

Type Parameters ​

Type Parameter
T

Parameters ​

ParameterTypeDescription
itemsWeightedChoice<T>[]Weighted values / 带权重的选项

Returns ​

T

Selected value / 选中的值

Example ​

ts
randomWeighted([{ value: 'a', weight: 3 }, { value: 'b', weight: 1 }]) // => often 'a'

Since ​

1.0.0

Released under the MIT License.