DUSTY FIELDS

2016.12.16

Swift 101: Generate Random Numbers

There are several different ways to generate random numbers in Swift. But a quick and easy way is to use the arc4random method. For example:

let myNumber = arc4random_uniform(6)

This will generate a random number between 0 and 5. You can change the number in the parentheses to any Integer but it’s important to remember that 0 is counted in the range so using 6 will have a range of 0-5, 22 will have a range of 0-21, etc.