Skip to content
Snippets Groups Projects
Commit 711d71c8 authored by Hachem_Muche's avatar Hachem_Muche
Browse files

More robust and efficient rng.rarityTable function that handles floating point...

More robust and efficient rng.rarityTable function that handles floating point rarity values properly and handles 0 or empty rarity values gracefully.
parent accfd911
No related branches found
No related tags found
1 merge request!347Improved rng.rarityTable function
......@@ -2257,18 +2257,27 @@ function rng.poissonProcess(k, turn_scale, rate)
return math.exp(-rate*turn_scale) * ((rate*turn_scale) ^ k)/ util.factorial(k)
end
--- Randomly select a table from a list of tables based on rarity
-- @param t <table> indexed table containing the tables to choose from
-- @param rarity_field <string, default "rarity">, field in each table containing its rarity value
-- rarity values are numbers > 0, such that higher values reduce the chance to be selected
-- @raturn the table selected, index
function rng.rarityTable(t, rarity_field)
if #t == 0 then return end
rarity_field = rarity_field or "rarity"
local max = 0
for _, e in ipairs(t) do max = max + e[rarity_field] end
local rt = {}
for _, e in ipairs(t) do
local nb = math.floor(max / e[rarity_field])
for i = 1, nb do rt[#rt+1] = e end
rarity_field = rarity_field or "rarity"
local total, val = 0
for i, e in ipairs(t) do
val = e[rarity_field]; val = val and 1/val or 0
total = total + val
rt[i] = total
end
val = rng.float(0, total)
for i, total in ipairs(rt) do
if total >= val then
return t[i], i
end
end
return rng.table(rt)
end
function util.show_function_calls()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment