mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-10 02:31:48 +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)
|
or _global_random(min, max)
|
||||||
end
|
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)
|
--pick a random value from a table (or nil if it's empty)
|
||||||
function tablex.pick_random(t, r)
|
function tablex.pick_random(t, r)
|
||||||
if #t == 0 then
|
if #t == 0 then
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
return t[_random(1, #t, r)]
|
return t[tablex.random_index(t, r)]
|
||||||
end
|
end
|
||||||
|
|
||||||
--take a random value from a table (or nil if it's empty)
|
--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
|
if #t == 0 then
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
return table.remove(t, _random(1, #t, r))
|
return table.remove(t, tablex.random_index(t, r))
|
||||||
end
|
end
|
||||||
|
|
||||||
--shuffle the order of a table
|
--shuffle the order of a table
|
||||||
|
Loading…
Reference in New Issue
Block a user