Hi guys, i have question.
This is my arraylist.
local t = {} t['a'] = 'a'; t['b'] = 'b'; t['c'] = 'c'; t['d'] = 'd';
In the loop with pairs and always return
a a d d c c b b
How can i shuffle this ? I want to change the order. Like this;
d d c c b b a a
thanks in advance.
[lua]
local t = {“a”,“b”,“c”,“d”} – original order
– shuffle entries
for i = 1, #t*2 do – repeat this for twice the amount of elements in the table, to make sure everything is shuffled well
local a = math.random(#t)
local b = math.random(#t)
t[a],t[b] = t[b],t[a]
end
– print output of shuffled order
for i = 1, #t do
print(t[i])
end
[/lua]
Untested but something like this should work.
[lua]
local t = {“a”,“b”,“c”,“d”} – original order
– shuffle entries
for i = 1, #t*2 do – repeat this for twice the amount of elements in the table, to make sure everything is shuffled well
local a = math.random(#t)
local b = math.random(#t)
t[a],t[b] = t[b],t[a]
end
– print output of shuffled order
for i = 1, #t do
print(t[i])
end
[/lua]
Untested but something like this should work.