Dalibor Dalibor - 1 year ago 147
Swift Question

Generate a random double between -1 and 1 in Swift

This one should be quite simple but somehow I can't find a way to do this:
I have to get a random double between -1 and 1.

Everything I found on the internet was either for Integers, from 0 to 1 or didn't work.

I hope some of you guys can help me!

Hoa Hoa
Answer Source
let a = (Double(arc4random()) / Double(UInt32.max)) * 2 - 1

PS: use UInt32.max for a Swifty code ;)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download