you can add an array of seen index like below and get the random values each time until all array items are fetched randomly. You will get the Idea from this and implement your own way to do it properly as you desire
//The lists of items
let items = ["blue","red","purple", "gold", "yellow", "orange","light blue", "green", "pink", "white", "black"]
//array to hold the index we have already traversed
var seenIndex = [Int]()
func chooseRandom() -> String {
if seenIndex.count == items.count { return "" } //we don't want to process if we have all items accounted for (Can handle this somewhere else)
let index = Int(arc4random_uniform(UInt32(items.count))) //get the random index
//check if this index is already seen by us
if seenIndex.contains(index) {
return chooseRandom() //repeat
}
//if not we get the element out and add that index to seen
let requiredItem = items[index]
seenIndex.append(index)
return requiredItem
}
Another approach is listed below. This will also give you random/unique values but without using recursion
var items = ["blue","red","purple", "gold", "yellow", "orange","light blue", "green", "pink", "white", "black"]
//This will hold the count of items we have already displayed
var counter = 0
func ShowWord() {
//we need to end if all the values have been selected
guard counter != items.count else {
print("All items have been selected randomly")
return
}
//if some items are remaining
let index = Int(arc4random_uniform(UInt32(items.count) - UInt32(counter)) + UInt32(counter))
//set the value
randomWord?.text = items[index]
//printing for test
debugPrint(items[index])
//swap at the place, so that the array is changed and we can do next index from that index
items.swapAt(index, counter)
//increase the counter
counter += 1
}
Yet another way would be to take advantage of swap function. The following things are happening here
1. We create a variable to know how many items we have selected/displayed from the list i.e var counter = 0
2. If we have selected all the elements then we simply return from the method
3. When you click the button to select a random value, we first get the random index in the range from 0 to array count
4. Set the random item to randomWord i.e display or do what is intended
5. now we swap the element
e.g if your array was ["red", "green", "blue"] and the index was 2 we swap "red" and "blue"
6. Increase the counter
7. when we repeat the process the new array will be ["blue", "green", "red"]
8. The index will be selected between _counter_ to _Array Count_