added tablex.random_index

This commit is contained in:
Max Cahill 2021-07-06 08:29:45 +10:00
parent 20aa83f79e
commit 0691f2bd36

View File

@ -161,12 +161,20 @@ local function _random(min, max, r)
or _global_random(min, max)
end
--pick a random value from a table (or nil if it's empty)
function tablex.random_index(t, r)
if #t == 0 then
return 0
end
return _random(1, #t, r)
end
--pick a random value from a table (or nil if it's empty)
function tablex.pick_random(t, r)
if #t == 0 then
return nil
end
return t[_random(1, #t, r)]
return t[tablex.random_index(t, r)]
end
--take a random value from a table (or nil if it's empty)
@ -174,7 +182,7 @@ function tablex.take_random(t, r)
if #t == 0 then
return nil
end
return table.remove(t, _random(1, #t, r))
return table.remove(t, tablex.random_index(t, r))
end
--shuffle the order of a table