Imran Imran - 3 months ago 5
Swift Question

Swift- Generating random values between two Integer values

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

let randomNumber = arc4random_uniform(10)
println(randomNumber)


But I need to print values between 10 to 50, Please suggest me....

Thanks...

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)
Comments