我想创建一个表格,每一行都包含某种权重。然后我想选择概率等于(该行的权重)/(所有行的权重)的随机值。例如,如果有 5 行权重为 1,2,3,4,5(满分 1000),我将得到第一行的大约 1/15*1000=67 倍,依此类推。
该表将被手动填写。然后我会从中取一个随机值。但我希望有能力改变填充阶段的概率。
最佳答案
我找到了这个 nice little algorithm在 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
关于sql - 如何将 "weights"添加到MySQL表中并根据这些选择随机值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1819293/