Skip to content

Instantly share code, notes, and snippets.

@irfan-dahir
Created July 7, 2017 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irfan-dahir/784cbccf92c588f3fa4882d05198df31 to your computer and use it in GitHub Desktop.
Save irfan-dahir/784cbccf92c588f3fa4882d05198df31 to your computer and use it in GitHub Desktop.
Shuffles your table arbitrarily
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