Im trying to generate CGFloat random but do not know how it is done in swift 3. What is CGFloat.random(min: CGFloat, max: CGFloat) of swift 2 equivalent in swift 3? And where can I find changed swift functions from swift 2 to swift 3?
Asked
Active
Viewed 1,309 times
1 Answers
1
From https://stackoverflow.com/a/28075467/2383604 use this:
public extension CGFloat {
/// Randomly returns either 1.0 or -1.0.
public static var randomSign:CGFloat {
get {
return (arc4random_uniform(2) == 0) ? 1.0 : -1.0
}
}
/// Returns a random floating point number between 0.0 and 1.0, inclusive.
public static var random:CGFloat {
get {
return CGFloat(arc4random())
}
}
/**
Create a random num CGFloat
- parameter min: CGFloat
- parameter max: CGFloat
- returns: CGFloat random number
*/
public static func random(min: CGFloat, max: CGFloat) -> CGFloat {
return CGFloat.random * (max - min) + min
}
}
CGFloat.random
CGFloat.random(min: 0.1, max: 0.2)

Community
- 1
- 1

matt.writes.code
- 674
- 10
- 22