2016-11-17 93 views

回答

0

https://stackoverflow.com/a/28075467/2383604使用本:

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)