Depending on the application, it might be better to just choose one part and then store the rest in a separate table. So instead of looping over each of the table's component you'd only need to loop n amount of times, with n being the amount of components you want to move to the another table.
Here's the concept with 4 components:
math.randomseed(tick())
components = {"A","B","C","D"}
function randomlychoose(inputtable,n)
local temptable = inputtable
local outputtable = {}
for i = 1,n do
r = math.random(1,#temptable)
table.insert(outputtable,#outputtable + 1,temptable[r])
table.remove(temptable,r)
end
return temptable,outputtable
end
selection,rest = randomlychoose(components,1)
print(selection[1])
print(rest[1],rest[2],rest[3])
Output:
B
A,C,D
Although this solution is quite more convoluted than what is needed, it will run quite efficiently no matter how many components you add to the list.
1 comment
That would significantly limit the number of random assortments.