Imran Imran - 1 year ago 135
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)

But I need a value between 10 and 50.

Answer Source

try this

let randomNumber = arc4random_uniform(40) + 10

in general form

let lower : UInt32 = 10
let upper : UInt32 = 50
let randomNumber = arc4random_uniform(upper - lower) + lower
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download