local WeightedRandom = {}

function WeightedRandom.PickId(rng, entries)
	local total = 0
	for _, entry in ipairs(entries) do
		total += entry.weight
	end
	if total <= 0 then
		error("WeightedRandom.PickId: non-positive total weight")
	end

	local roll = rng:NextNumber(0, total)
	local cumulative = 0
	for _, entry in ipairs(entries) do
		cumulative += entry.weight
		if roll < cumulative then
			return entry.id
		end
	end

	return entries[#entries].id
end

return WeightedRandom
