--[[
	Most accurate way to calculate cumulative probability. Accepts a list of integers (any size).
	Used for lootboxes, drop chances, or any other list-oriented RNG mechanics. Highly accurate. Returns (1) winner ID and (2) roll.
	
	Example:
		_L.Functions.Lottery(
			{"dirt", 500},
			{"copper", 45},
			{"silver", 18},
			{"gold", 7.5},
			{"platinum", 0.05},	
		)
--]]



return function(...)
	--- Variables
	local entrees = {...}
	local roll
	
	--- Needs a minimum of 1 entree
	if #entrees < 2 then
		--_L.Print("Did not provide enough arguments to simulate lottery (>1)", true)
		return (entrees[1] and entrees[1][1])
	end
	
	--- Convert integers into a percentage (int/total)
	local function Init()
		local total = 0
		--
		for _, entree in ipairs(entrees) do
			local float = entree[2]
			total = total + float
		end
		--
		for _, entree in ipairs(entrees) do
			local float = entree[2]
			local percentage = (float / total)
			entree[2] = percentage
			entree[3] = float
		end
	end
	
	--- Pick a random number (0-1)
	local function Roll()
		roll = rng:NextNumber()
	end
	
	--- Pick winner (actual black magic)
	local function Calc()
		local total = 0
		--
		for _, entree in ipairs(entrees) do
			local percentage = entree[2]
			total = total + percentage
			if roll <= total then
				return entree
			end
		end
	end
	
	--- Run
	Init()
	Roll()
	local result = Calc()
	
	--
	return result[1], roll, result[3]
end
