📚 Native/WeightRandom

加权随机类是开发阶段常用功能,可进行加权重随机相关执行,在抽奖、物资概率等需求中比较常用。

此类仅用于int整数的随机生成,实际应用其他类型可以封装数组+索引来实现。

let wr = WeightRandom.Create();
wr.SetItem(0, 50);
wr.SetItem(1, 25);
wr.SetItem(2, 5);
 
for (let i = 0; i < 20; i++) {
	DLog(wr.Random());
}
 
//Random Result: 0(50%) > 1(25%) > 2(5%)

📒 Static Function

📘 WeightRandom.Create()

创建一个加权随机类,可提供随机种子(默认随机)。

function WeightRandom.Create( seed: int = 0 ): WeightRandom

📒 Property

📘 weightRandom.Count

获取随机池值的数量。

weightRandom.Count: int

📘 weightRandom.Weight

获取随机池值的总权重。

weightRandom.Weight: int

📒 Function

📘 weightRandom.SetItem()

增加\设置指定值的权重,权重越高,概率越大

function weightRandom.SetItem( value: int, weight: int )

📘 weightRandom.RemoveItem()

从随机池中移除指定值。

function weightRandom.RemoveItem( value: int )

📘 weightRandom.Random()

根据当前权重随机取值。

function weightRandom.Random(): int

📘 weightRandom.Clear()

清空随机池全部值。

function weightRandom.Clear()