In Swift 4.2 we do not longer have to use functions like arc4random provided through C APIs to generate random numbers. Because Apple’s Swift Team added a set of new APIs to make this easier.

All numeric types in Swift 4.2 (like Int or Float) define a random method that takes a range and returns a random number:

let newRandomInt = Int.random(in: 0 ..< 100)

This works also with array elements, because there has been added a randomElement() method to the collection protocol:

let towns = ["Portland", "Chicago", "Corpus Christi"]

As you can see this returns an optional.

And there is a shuffled() method that returns a shuffled representation of the array elements.

let randomlyOrderedTowns = ["Portland", "Chicago", "Corpus Christi"]