diff --git a/utils/table.lua b/utils/table.lua
index d651ea32..e4f6e2a1 100644
--- a/utils/table.lua
+++ b/utils/table.lua
@@ -143,7 +143,7 @@ end
--- Chooses a random entry from a weighted table
-- because this uses math.random, it cannot be used outside of events
--- @param weight_table
of tables with items and their weights
+-- @param weighted_table of tables with items and their weights
-- @param item_index of the index of items, defaults to 1
-- @param weight_index of the index of the weights, defaults to 2
-- @return table element
@@ -167,6 +167,24 @@ function table.get_random_weighted(weighted_table, item_index, weight_index)
end
end
+--- Returns a table with % chance values for each item of a weighted_table
+-- @param weighted_table of tables with items and their weights
+-- @param item_index of the index of items, defaults to 1
+-- @param weight_index of the index of the weights, defaults to 2
+function table.get_random_weighted_chances(weighted_table, item_index, weight_index)
+ local total_weight = 0
+ item_index = item_index or 1
+ weight_index = weight_index or 2
+ for _, v in pairs(weighted_table) do
+ total_weight = total_weight + v[weight_index]
+ end
+ local chance_table = {}
+ for k, v in pairs(weighted_table) do
+ chance_table[k] = v[weight_index] / total_weight
+ end
+ return chance_table
+end
+
--- Creates a fisher-yates shuffle of a sequential number-indexed table
-- because this uses math.random, it cannot be used outside of events if no rng is supplied
-- from: http://www.sdknews.com/cross-platform/corona/tutorial-how-to-shuffle-table-items