feat(table): added table.shuffle (#718)

This commit is contained in:
Slyy
2025-03-07 05:16:42 +01:00
committed by GitHub
parent 32d3fb05c7
commit cf5423e42f

View File

@@ -103,10 +103,23 @@ local function table_merge(t1, t2, addDuplicateNumbers)
return t1
end
---@param tbl table
---@return table
---Shuffles the elements of a table randomly using the Fisher-Yates algorithm.
local function shuffle(tbl)
local len = #tbl
for i = len, 2, -1 do
local j = math.random(i)
tbl[i], tbl[j] = tbl[j], tbl[i]
end
return tbl
end
table.contains = contains
table.matches = table_matches
table.deepclone = table_deepclone
table.merge = table_merge
table.shuffle = shuffle
local frozenNewIndex = function(self) error(('cannot set values on a frozen table (%s)'):format(self), 2) end
local _rawset = rawset