SALE ENDS TODAY: Save 50% on all Swift books and bundles! >>

Generating random numbers with GameplayKit: GKRandomSource

Let's look at the most basic way of generating random numbers using GameplayKit, which is the GKRandomSource class and its sharedRandom() method. Of course, this means adding an import for GameplayKit into the playground, so please do that now.

A random source is a provider of an unfiltered stream of random numbers as you need them. As you'll see soon, GameplayKit has various options for your stream, but for now we're going to look at the simplest one: sharedRandom().

Using sharedRandom() for a random number source returns the systems built-in random source that's used for a variety of other tasks, which means you can be pretty sure it's in a truly random state by the time it gets to you. It does, however, mean that it's useless for synchronizing network games, because everyone's device is in a different state.

To produce a truly random number you'd use the nextInt() method like this:


That produces a number between -2,147,483,648 and 2,147,483,647 – yes, that's a negative number, which means it's not a drop-in replacement for arc4random(). Plus, even with GameplayKit's great new logic, Apple includes a warning that it's not guaranteed to be random for very specific situations, so for both these reasons it's not likely you'll want to use nextInt() much.

As an alternative, try using the nextInt(upperBound:) method, which works identically to arc4random():

print(GKRandomSource.sharedRandom().nextInt(upperBound: 6))

That will return a random number from 0 to 5 using the system's built-in random number generator.

As well as nextInt() and nextInt(upperBound:) are nextBool() for generating a random true/false value and nextUniform() for generating a random floating-point number between 0 and 1. Both of these are implemented using nextInt(upperBound:) so they output properly random numbers.

Note: If you’re just generating simple random values, using Swift’s Int.random(in:), Float.random(in:), Double.random(in:), and Bool.random() are much easier than using GameplayKit. However, GameplayKit does have the advantage that you can shape its random output, as we’ll look at next…

SPONSORED Instabug helps you identify and resolve severe crashes quickly. You can retrace in-app events and know exactly which line of code caused the crash along with environment details, network logs, repro steps, and the session profiler. Ask more questions or keep users up-to-date with in-app replies straight from your dashboard. Instabug takes data privacy seriously, so no one sees your data but you! See more detailed features comparison and try Instabug's crash reporting SDK for free.

Save 50% on all books and bundles

The biggest ever Hacking with Swift sale is now on, letting you save 50% on all books and bundles. Learn something new with Swift and enjoy great savings while the sale lasts!

Click here to save 50% in our Black Friday sale!

Buy Pro Swift 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 (Vapor Edition) 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 Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5