mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-22 06:04:35 +00:00
added tablex.random_index
This commit is contained in:
parent
20aa83f79e
commit
0691f2bd36
12
tablex.lua
12
tablex.lua
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user