How do i randomize a table?

I want this table to be randomized, is there any way how too?

AttackOrder = {"Bonewall","Bonewall2","Bonewall3","Bluewall"}
2 Likes

I am sure there is some algorithm or something for this, what I would so is I would loop through a table, chose a random value from that table then add it to another table. Then return the new table.

1 Like

You can do

local Table = {"GAmer", "Chad", "You fat donkey!"}
local RandomizedTable = {}
for i, v in pairs(Table) do
      local RandomItem = Table[math.random(1, #Table)]
      table.remove(Table, table.find(Table, RandomItem))
      table.insert(RandomizedTable, 1, RandomItem)
end

Now you have a table with all the items randomized! Also don’t mind the donkey part in the table I just watch too much gordon ramsay

10 Likes
function RandomizeTable(tbl)
	local returntbl={}
	if tbl[1]~=nil then
		for i=1,#tbl do
			table.insert(returntbl,math.random(1,#returntbl+1),tbl[i])
		end
	end
	return returntbl
end

taken from Randomize Table Function - Roblox

7 Likes

You should use the Fisher-Yates shuffle instead. The methods posted here aren’t uniform.

local function shuffle(t)
	local j, temp
	for i = #t, 1, -1 do
		j = math.random(i)
		temp = t[i]
		t[i] = t[j]
		t[j] = temp
	end
end
30 Likes

Here you go;

local AttackOrder = {"Bonewall","Bonewall2","Bonewall3","Bluewall"}
print(AttackOrder[math.random(1,#AttackOrder)])
4 Likes