Pick a random element from an array
Swift 4.2 and above The new recommended approach is a built-in method on the Collection protocol: randomElement(). It returns an optional to avoid the empty case I assumed against previously. let array = [“Frodo”, “Samwise”, “Merry”, “Pippin”] print(array.randomElement()!) // Using ! knowing I have array.count > 0 If you don’t create the array and aren’t … Read more