我想创建一个表,每行都包含某种权重。然后我想选择概率等于(该行的权重)/(所有行的权重)的随机值。例如,如果有 5 行,权重为 1000 中的 1,2,3,4,5,我会得到大约 1/15*1000=67 倍第一行,依此类推。
该表需要手动填写。然后我会从中取出一个随机值。但我希望有能力改变填充阶段的概率。
我找到了这个不错的小算法在Quod Libet。您或许可以将其转换为某种过程 SQL。
function WeightedShuffle(list of items with weights):
max_score ← the sum of every item’s weight
choice ← random number in the range [0, max_score)
current ← 0
for each item (i, weight) in items:
current ← current + weight
if current ≥ choice or i is the last item:
return item i
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)