From cf5423e42f1d59fa5c64eec74becf6fae069f163 Mon Sep 17 00:00:00 2001 From: Slyy Date: Fri, 7 Mar 2025 05:16:42 +0100 Subject: [PATCH] feat(table): added table.shuffle (#718) --- imports/table/shared.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/imports/table/shared.lua b/imports/table/shared.lua index 92f08d5..d35c2f7 100644 --- a/imports/table/shared.lua +++ b/imports/table/shared.lua @@ -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