Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
sh
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) local total_rate = 0 for k, v in ipairs(items) do total_rate = total_rate + v.w end local rates = {} for k, v in ipairs(items) do rates[#rates + 1] = {id = v.id, rate = v.w / total_rate} end for k = 1, #rates do if k == 1 then goto LABEL end rates[k].rate = rates[k - 1].rate + rates[k].rate ::LABEL:: end print(dump(rates))
run
|
edit
|
history
|
help
0
Simple Lua primality test
anti.lua
Hakusu
ROBLOX Ui Play Button
Fibonacci in Lua (Inspired)
Random number generation with ranges (Lua)
bf-asm compiler test 2
Monty Hall Problem Simulated
hij
Frequency distribution of rolling a certain number on xDy