Imran Imran - 2 months ago 6
Swift Question

Generating random values in Swift between two integer values

I'm trying to generate random values between two integers. I've tried this, which starts from 0,

let randomNumber = arc4random_uniform(10)
println(randomNumber)


But I need a value between 10 and 50.

Answer

try this

let randomNumber = arc4random_uniform(40) + 10
println(randomNumber)

in general form

let lower : UInt32 = 10
let upper : UInt32 = 50
let randomNumber = arc4random_uniform(upper - lower) + lower
println(randomNumber)