iOS Game development tips

Using GameplayKit to help your game development.

Randomness

Have you ever had to implement your own random methods?

With GameplayKit, Apple gave to developers a new way of generating random numbers through GKRandomDistribution and it’s subclasses: GKGaussianDistribution, and GKShuffledDistribution.

Let’s talk a bit about them and see some examples!


GKRandomDistribution:

"produces a uniform distribution across a specified range"

Need a simple random? Ok, go for it!

GKGaussianDistribution:

“Random samplings are most likely to result in a value near the center of the distribution, and have reduced probability of producing higher or lower values.”

This will give you mostly the mean numbers of a given range. In the case below, if you toss the d6, there’s a high chance of it to give you a 3 or 4.

GKShuffledDistribution:

“Prevents clusters of similar values from occurring in sequence”

Aka "fair randomization", it will try to give you no repeated numbers in a row.


Examples:

Usually when we are developing a game, and we need a random number in order to have a hitpoint taken or a dice rolled and so on, we need to implement our own methods. But, how accurate and fair are they? And how about repetition?

Let’s say you are developing a RPG game and you are implementing a battle system based on dice rolls. What if I told y0u can roll a D6 or even a D20 with a single line of code?

import GameplayKit
//Rolling a d6
let
diceRolled = GKRandomDistribution.d6().nextInt()
//Rolling a d20
let
hitPoint = GKRandomDistribution.d20().nextInt()
//Need a bigger dice? No problem! Here’s a d100!
let
d100 = GKRandomDistribution(lowestValue: 1, highestValue: 100)
let
ultraDiceRoll = d100.nextInt()

Custom distributions

If you need to customize your randomness even more, there are three types of random sources that can help us to reach different behaviors. They are:

  • GKARC4RandomSource()
  • GKMersenneTwisterRandomSource()
  • GKLinearCongruentialRandomSource()

GKARC4RandomSource

Similar to arc4random

Basic random number generation using ARC4 algorithm.

let arcSource = GKARC4RandomSource()
let
random = GKShuffledDistribution(randomSource: arcSource, lowestValue: 1, highestValue: 6)
let myARCRandomNumber = random.nextInt()
//You can ask for booleans as well!
let myRandomBool = random.nextBool()

GKMersenneTwisterRandomSource()

Slower algorithm, more random

Like the random source above, this will generate a random number but using the Mersenne Twister algorithm.

let twisterSource = GKMersenneTwisterRandomSource()
let
random = GKShuffledDistribution(randomSource: twisterSource, lowestValue: 1, highestValue: 6)
let mySuperRandomNumber = random.nextInt()
//You can ask for booleans as well!
let mySuperRandomBool = random.nextBool()

GKLinearCongruentialRandomSource()

Faster algorithm, less random

A random number generator that implements the Linear Congruential Generator algorithm.

let linearSource = GKMersenneTwisterRandomSource()
let
random = GKShuffledDistribution(randomSource: linearSource, lowestValue: 1, highestValue: 6)
let myRandomNumber = random.nextInt()
//You can ask for booleans as well!
let myRandomBool = random.nextBool()

Here’s a picture that I took of my playground running some .nextInt() for each one of those random sources using GKRandomDistribution:

Screenshot of my playground

Can you see how different they behave ? You can also try to use the other distributions in order to see the differences between them.


For now, that’s it, folks! You can check the Apple documentation about GameplayKit, which has a LOT of cool stuff that will help you to improve your game development and hopefuly will save a lot of time :)

Kind Regards,
Carlos.

Like what you read? Give Carlos Corrêa a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.