Creating Random Numbers in Swift

There are several variations of arc4random() that can be used to create random numbers in Swift. Two common examples follow.

let randomNum = arc4random()
println("randomNum: \(randomNum)")

randomNum: 2078946146

Create a random number less than a specified upper bound:

let x = arc4random_uniform(100)
println("x: \(x)")

x: 5

arc4random is part of the Standard C library. Type ‘man arc4random’ in a terminal to read more.