Unique Random Values
by:
cyrus01337- Make a copy of the array
- Grab a random element from the table, then remove it from the copy
- Repeat step 2
n
times forn
random elements
local TOTAL_UNIQUE_ELEMENTS = 3
local array = {"foo", "bar", "baz", "quo"}
local copy = table.clone(array)
local uniqueElements = {}
for i = 1, TOTAL_UNIQUE_ELEMENTS do
local randomIndex = math.random(#copy)
local randomElement = array[randomIndex]
table.insert(uniqueElements, randomElement)
table.remove(copy, randomIndex)
end
...