local function shuffleTable( t )
local rand = math.random
assert( t, “shuffleTable() expected a table, got nil” )
local iterations = #t
local j
for i = iterations, 2, -1 do
j = rand(i)
t[i], t[j] = t[j], t[i]
end
end
I have found this code when searching for an algorithm for card shuffling, after several attempts to create one on my own. It works perfectly, but I need someone to explain it to me, the mentality behind this piece of code.
Thanks in advance.