Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
cumulative probability
function dump (tbl, indent) if not indent then indent = 0 end local toprint = string.rep(" ", indent) .. "{\r\n" indent = indent + 2 for k, v in pairs(tbl) do toprint = toprint .. string.rep(" ", indent) if (type(k) == "number") then toprint = toprint .. "[" .. k .. "] = " elseif (type(k) == "string") then toprint = toprint .. k .. "= " end if (type(v) == "number") then toprint = toprint .. v .. ",\r\n" elseif (type(v) == "string") then toprint = toprint .. "\"" .. v .. "\",\r\n" elseif (type(v) == "table") then toprint = toprint .. dump(v, indent + 2) .. ",\r\n" else toprint = toprint .. "\"" .. tostring(v) .. "\",\r\n" end end toprint = toprint .. string.rep(" ", indent-2) .. "}" return toprint end local items = { [1] = {id = "bandage", w = 2}, [2] = {id = "bow", w = 4}, [3] = {id = "gold", w = 1}, [4] = {id = "apple", w = 5} } table.sort( items, function(a, b) return a.w < b.w end) function raffle(table) local total_rate = 0 for i = #table, 1, -1 do total_rate = total_rate + table[i].w end math.randomseed(os.time()) local c = 0 -- cumulative probability local randf = math.random() for i = #table, 1, -1 do c = c + table[i].w / total_rate if (randf <= c) then return table[i] end end end print(raffle(items).id)
run
|
edit
|
history
|
help
0
python like round function in lua
ok
Conta valores acima da média - LUA
Enums in Lua (library)
Hand positions in a circle
Key
PAID HACK PuBG
Frequency distribution of rolling a certain number on xDy
Hello 2
PAID HACK PuBG