Some times you got an array like this:
and you HAVE to sort this randomly, you cant just call arr[math.random(1,#arr)], sou what you do?
You can declare like 10 of this same array and call one random of these or sort all them easly.
This code basicaly copy the content of arr to another array. I cant just use sorted = arr because in some metatables form lua if you do this you going to use sorted as a pointer to content of arr. If you dont know what i am sayig, trust me, in some cases without what i did it is going to be wrong.
so then it run a for size^2 to arr, then a random runs taking 2 possible positions. So the code change the content of this positions. if selected 5 an 7 position and inside is "hai" and "bye", so the in 5 will contain "bye" in 7 "hai"
As it run #arr^2 times the arr will be random sorted
Code:
arr = {
{'bla',3,7},
{'ble',{4,5},5},
{'omg',0,2},
{whatever},
...
{n},
}
and you HAVE to sort this randomly, you cant just call arr[math.random(1,#arr)], sou what you do?
You can declare like 10 of this same array and call one random of these or sort all them easly.
Code:
function randomSort(arr)
local sorted = {}
local rand2
local rand
local mem
for i=1,#arr do
sorted[i] = arr[i]
end
if (#arr <= 1) then
return sorted;
end
for i=1,(#arr)^2 do
repeat
rand = math.random(1,#sorted)
rand2 = math.random(1,#sorted)
until rand ~= rand2
mem = sorted[rand]
sorted[rand] = pgtss[rand2]
sorted[rand2] = mem
end
return sorted
end
This code basicaly copy the content of arr to another array. I cant just use sorted = arr because in some metatables form lua if you do this you going to use sorted as a pointer to content of arr. If you dont know what i am sayig, trust me, in some cases without what i did it is going to be wrong.
so then it run a for size^2 to arr, then a random runs taking 2 possible positions. So the code change the content of this positions. if selected 5 an 7 position and inside is "hai" and "bye", so the in 5 will contain "bye" in 7 "hai"
As it run #arr^2 times the arr will be random sorted