UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to generate random numbers in Swift

Smart, simple, and secure new ways to generate randomness

Paul Hudson       @twostraws

Swift 4.2 implemented SE-0202: Random Unification, which introduced a new random API that’s native to Swift. This means you can mostly stop using arc4random_uniform() and GameplayKit to get randomness, and instead rely on a cryptographically secure randomizer that’s baked right into the core of the language.

Let’s dive in with one of the most common use cases for randomness: generating random numbers. This is done by calling the random() method on whatever numeric type you want, providing the range you want to work with. For example, this generates a random number in the range 1 through 4, inclusive on both sides:

let randomInt = Int.random(in: 1..<5)

Similar methods exist for Float, Double, and CGFloat:

let randomFloat = Float.random(in: 1..<10)
let randomDouble = Double.random(in: 1...100)
let randomCGFloat = CGFloat.random(in: 1...1000)

There’s even one for booleans, generating either true or false randomly:

let randomBool = Bool.random()

Checking a random boolean is effectively the same as checking Int.random(in: 0...1) == 1, but it expresses your intent more clearly.

Best of all, SE-0202 included support for shuffling arrays. Previously I used GameplayKit for this, partly because it was easier than teaching newbies how to shuffle arrays well, and partly because GameplayKit has a number of advanced shuffling algorithms that let you shape your data.

However, GameplayKit’s shuffling implementation is imported really poorly into Swift: you need to typecast your Swift array to an NSArray then force-cast the result back to your original type, like this:

let numbers = [1, 2, 3]
let shuffledNumbers = (numbers as NSArray).shuffled() as! [Int]

This is something I have complained about extensively on previous occasions – I even managed to get a screenshot of my Radar on this issue used as part of a 9to5mac article in the hope that someone from Apple would notice and fix the issue.

Well, at last the problem is being tackled head on: Swift 4.2 added new shuffle() and shuffled() methods to arrays, depending on whether you want in-place shuffling or not. For example:

var albums = ["Red", "1989", "Reputation"]

// shuffle in place
albums.shuffle()

// get a shuffled array back
let shuffled = albums.shuffled()

It also adds a new randomElement() method to arrays, which returns one random element from the array if it isn’t empty, or nil otherwise:

if let random = albums.randomElement() {
    print("The random album is \(random).")
}

Having such a simple and secure random API built right into the core of our language is guaranteed to help many developers write better code, and it’s a really welcome feature.

The implementation of SE-0202 doesn’t mean things like GameplayKit are redundant, though – Swift only has one random number generator at its core, whereas GameplayKit supports more advanced functionality such as Gaussian distribution and “fair” randomnmess.

That being said, we are given the ability to implement our own random number generators to work alongside all the methods listed above, so it’s possible we might see some open source projects step in to fill the void.

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.7/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.