Created
July 7, 2017 18:01
-
-
Save irfan-dahir/784cbccf92c588f3fa4882d05198df31 to your computer and use it in GitHub Desktop.
Shuffles your table arbitrarily
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function table.shuffle(input, debug) | |
if not type(input) == "table" then print("table.shuffle ERROR: <input> must be array") return nil end | |
if not type(debug) == "nil" or not type(debug) == "boolean" then print("table.shuffle ERROR: <debug> must be nil or boolean") return nil end | |
if debug == nil then debug = false end -- if debug is true, it'll print the output | |
math.randomseed(os.time()) | |
local output = {} | |
while true do | |
math.random() math.random() math.random() | |
local chosen = input[math.random(1,#input)] | |
local exists = false | |
for i=1,#output do if output[i] == chosen then exists = true end end | |
if not exists then table.insert(output, chosen) end | |
if #output == #input then break end | |
end | |
if debug then | |
for i=1,#output do | |
print(output[i]) | |
end | |
end | |
return output | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment