Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to choose different random values from a table?

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

2 answers

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.

2 comments

0

Without removing values from the table?

0

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().

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.