From 7eb6bcc576e4a5bfdc68c32da4733916fad1bc74 Mon Sep 17 00:00:00 2001 From: Jesse Viikari Date: Wed, 17 Nov 2021 20:20:04 +0200 Subject: [PATCH] Add a weighted random pick for tablex --- tablex.lua | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tablex.lua b/tablex.lua index effab38..a7067d6 100644 --- a/tablex.lua +++ b/tablex.lua @@ -202,6 +202,26 @@ function tablex.take_random(t, r) return table.remove(t, tablex.random_index(t, r)) end +--return a random index based on weights provided +--Example: { 0.3, 1, 6, 0.5 } (3rd index most likely) +function tablex.weighted_random(t, r) + if #t == 0 then + return nil + end + local sum = 0 + for _, weight in ipairs (t) do + sum = sum + weight + end + local rnd = _random(nil, nil, r) * sum + sum = 0 + for i, weight in ipairs (t) do + sum = sum + weight + if rnd <= sum then + return i, weight + end + end +end + --shuffle the order of a table function tablex.shuffle(t, r) for i = 1, #t do