大家好, 现有如下代码:
local WRandom = {}
WRandom.__index = WRandom
function WeightedRandom()
local tab = {}
tab.items = {}
setmetatable(tab, WRandom)
return tab
end
function WRandom:clear()
for i,_ in ipairs(self.items) do
table.remove(self.items, i)
end
end
function WRandom:Add(weight, item)
local t = {}
t.weight = weight
t.item = item
table.insert(self.items, t)
end
return WRandom
module(..., package.seeall)
local random = require "WRandom"
local s = WeightedRandom()