# 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?

importGameplayKit

//Rolling a d6diceRolled

let=GKRandomDistribution.d6().nextInt()

//Rolling a d20hitPoint

let= GKRandomDistribution.d20().nextInt()

//Need a bigger dice? No problem! Here’s a d100!d100 =

letGKRandomDistribution(lowestValue:1,highestValue:100)ultraDiceRoll = d100.

letnextInt()

### 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.

letarcSource =GKARC4RandomSource()random =

letGKShuffledDistribution(randomSource:arcSource,lowestValue:1, highestValue:6)letmyARCRandomNumber = random.nextInt()

//You can ask for booleans as well!letmyRandomBool = random.nextBool()

#### GKMersenneTwisterRandomSource()

*Slower algorithm, more random*

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

lettwisterSource =GKMersenneTwisterRandomSource()random =

letGKShuffledDistribution(randomSource:twisterSource,lowestValue:1, highestValue:6)letmySuperRandomNumber = random.nextInt()

//You can ask for booleans as well!letmySuperRandomBool = random.nextBool()

#### GKLinearCongruentialRandomSource()

*Faster algorithm, less random*

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

letlinearSource =GKMersenneTwisterRandomSource()random =

letGKShuffledDistribution(randomSource:linearSource,lowestValue:1, highestValue:6)letmyRandomNumber = random.nextInt()

//You can ask for booleans as well!letmyRandomBool = random.nextBool()

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

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.