diff options
author | HybridDog <3192173+HybridDog@users.noreply.github.com> | 2020-02-01 16:09:45 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-01 16:09:45 +0100 |
commit | ea5e231959365622607c8bfd953f6d96ec54a394 (patch) | |
tree | 67f379b77141ed94d5b37c87aac399e6a5ac01f6 /builtin/common | |
parent | 2b3490db1f0e99a427e34135770f8e5afcf275ce (diff) | |
download | minetest-ea5e231959365622607c8bfd953f6d96ec54a394.tar.gz minetest-ea5e231959365622607c8bfd953f6d96ec54a394.tar.bz2 minetest-ea5e231959365622607c8bfd953f6d96ec54a394.zip |
Add table.shuffle (#8299)
Diffstat (limited to 'builtin/common')
-rw-r--r-- | builtin/common/misc_helpers.lua | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/builtin/common/misc_helpers.lua b/builtin/common/misc_helpers.lua index e4c7f4aa3..1e9a08851 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 -------------------------------------------------------------------------------- |