I'm trying to shuffle a table's contents randomly. It works fine, except sometimes it doesn't return all the contents of the table. I printed out some of the keys of the table before shuffling, and they returned nil, but I'm not sure how to fix that. Here's the Lua:
local tab = {1,2,3,4,5,6,7,8,9,10}
function ReturnRandomTable(t)
local newt = {}
local i = 1
repeat
local rand = math.random(1,#t)
newt[i] = t[rand]
print(t[rand]) --sometimes prints nil
t[rand] = nil
i = i + 1
until #t == 0
return newt
end
table.shuffle = function(t)
local newt = ReturnRandomTable(t)
for i = #t,1,-1 do
t[i] = nil
end
return newt
end
local randt = table.shuffle(tab)
for _,v in pairs(randt) do
print(v)
end
Any help would be appreciated!