From 9c47c36741aacf212cb50fc74229548edd456aa3 Mon Sep 17 00:00:00 2001 From: HybridDog <3192173+HybridDog@users.noreply.github.com> Date: Sat, 1 Feb 2020 16:09:45 +0100 Subject: [PATCH] Add table.shuffle (#8299) --- builtin/common/misc_helpers.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/builtin/common/misc_helpers.lua b/builtin/common/misc_helpers.lua index e4c7f4a..1e9a088 100644 --- a/builtin/common/misc_helpers.lua +++ b/builtin/common/misc_helpers.lua @@ -576,6 +576,20 @@ function table.key_value_swap(t) end +function table.shuffle(t, from, to, random) + from = from or 1 + to = to or #t + random = random or math.random + local n = to - from + 1 + while n > 1 do + local r = from + n-1 + local l = from + random(0, n-1) + t[l], t[r] = t[r], t[l] + n = n-1 + end +end + + -------------------------------------------------------------------------------- -- mainmenu only functions --------------------------------------------------------------------------------