如何设置正确的逻辑以根据物品的稀有度(例如“稀有”“普通”)从列表中随机选择物品。

我正在使用lua语言中的Corona SDK编写游戏。我很难为这样的系统想出一种逻辑:

我有不同的物品。我希望一些物品有1/1000的选择机会(独特的物品),有一些有1/10,有一些有2/10等等。

我考虑填充一个表并选择一个随机物品。例如,我会在表中添加100个“X”物品,然后添加1个“Y”物品。因此,通过从[0,101]中随机选择,我可以实现我想要的效果,但我想知道是否有其他方法可以做到这一点。

点赞
用户501459
用户501459
items = {
    Cat     = { probability = 100/1000 }, -- 即 1/10
    Dog     = { probability = 200/1000 }, -- 即 2/10
    Ant     = { probability = 699/1000 },
    Unicorn = { probability =   1/1000 },
}

function getRandomItem()
    local p = math.random()
    local cumulativeProbability = 0
    for name, item in pairs(items) do
        cumulativeProbability = cumulativeProbability + item.probability
        if p <= cumulativeProbability then
            return name, item
        end
    end
end

你希望这些概率加起来得到 1。因此,如果你增加一个项的概率(或添加一个项),你需要从其他项中减去。这就是为什么我将 1/10 写成了 100/1000:当你有一个公共分母时,更容易看到物品的分布情况并进行更新。


你可以像这样确认自己得到了预期的分布:

local count = { }

local iterations = 1000000
for i=1,iterations do
    local name = getRandomItem()
    count[name] = (count[name] or 0) + 1
end

for name, count in pairs(count) do
    print(name, count/iterations)
end
2014-05-02 22:48:13
用户6778374
用户6778374

我认为这个答案更容易操作,尽管执行速度稍慢。

local chancesTbl = {
    -- 您可以使用任何非负整数填充这些
    -- 不必确保它们相加到任何特定值
    ["a"] = 2,
    ["b"] = 1,
    ["c"] = 3
}

local function GetWeightedRandomKey()
   local sum = 0
   for _, chance in pairs(chancesTbl) do
      sum = sum + chance
   end

   local rand = math.random(sum)
   local winningKey
   for key, chance in pairs(chancesTbl) do
      winningKey = key
      rand = rand - chance
      if rand <= 0 then break end
   end

   return winningKey
end
2016-12-15 14:18:22