local table = {"apple", "banana", "pear", "mango"}
for i = 1,3 do --How can I choose 3 different values from the table without picking the same one twice? end
local table = {"apple", "banana", "pear", "mango"}
for i = 1,3 do --How can I choose 3 different values from the table without picking the same one twice? end
local table = {"apple", "banana", "pear", "mango"}
local selectedValues = table
table.remove(selectedValues, math.random(#selectedValues))
If you want 3 out of your 4 values, simply remove a random value out of those 4.
local fruit = {"apple", "banana", "pear", "mango", "orange", "another banana"}
for i=1,3 do
local rand = math.random(1, #fruit)
local selectedFruit = fruit[rand]
table.remove(fruit, rand)
print("I ate",selectedFruit)
end
This discussion is preserved as it was. New answers, comments, and votes are closed.
2 comments
Without removing values from the table?
I'm removing values from a copy of your table, your original one remains unchanged. I don't see why you don't want to use table.remove().