Random Numbers, A simple way…
Creating random numbers can be very useful and there are a numbers of ways of going about this, I have found the simplest way is to use the new Gameplay Kit classes introduced in iOS 9.
let randomNumber = GKRandomSource.sharedRandom().nextInt()
This creates a random number that ranges between -2,147,483,648 and 2,147,483,647.
If you want something a bit more specific, then you can specify a user defined range that generates a random number between it.
let randomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(10)
This allows you to return a number between 0 – 9, so any number less than the limit set within the bracket.
You can also use the Objective C standard library method arc4Random()
to generate numbers that way too, but be careful to cater for the type being returned as UInt32,
Have fun 🙂