Swift随机浮动在0和1之间
在公共OSX Yosemite Beta中运行Xcode6-Beta4。
我试图得到0和1之间的随机浮动,但我似乎无法得到types转换工作。
func randomCGFloat() -> CGFloat { return CGFloat(arc4random()) / UINT32_MAX }
我得到一个“CGFloat”不能转换为“UInt8”的错误
尝试初始化除数作为浮点数,la:
CGFloat(Float(arc4random()) / Float(UINT32_MAX))
这是Int,Double,Float,CGFloat的 随机数的扩展
Swift 3&4语法
import Foundation import CoreGraphics // MARK: Int Extension public extension Int { /// Returns a random Int point number between 0 and Int.max. public static var random: Int { return Int.random(n: Int.max) } /// Random integer between 0 and n-1. /// /// - Parameter n: Interval max /// - Returns: Returns a random Int point number between 0 and n max public static func random(n: Int) -> Int { return Int(arc4random_uniform(UInt32(n))) } /// Random integer between min and max /// /// - Parameters: /// - min: Interval minimun /// - max: Interval max /// - Returns: Returns a random Int point number between 0 and n max public static func random(min: Int, max: Int) -> Int { return Int.random(n: max - min + 1) + min } } // MARK: Double Extension public extension Double { /// Returns a random floating point number between 0.0 and 1.0, inclusive. public static var random: Double { return Double(arc4random()) / 0xFFFFFFFF } /// Random double between 0 and n-1. /// /// - Parameter n: Interval max /// - Returns: Returns a random double point number between 0 and n max public static func random(min: Double, max: Double) -> Double { return Double.random * (max - min) + min } } // MARK: Float Extension public extension Float { /// Returns a random floating point number between 0.0 and 1.0, inclusive. public static var random: Float { return Float(arc4random()) / 0xFFFFFFFF } /// Random float between 0 and n-1. /// /// - Parameter n: Interval max /// - Returns: Returns a random float point number between 0 and n max public static func random(min: Float, max: Float) -> Float { return Float.random * (max - min) + min } } // MARK: CGFloat Extension public extension CGFloat { /// Randomly returns either 1.0 or -1.0. public static var randomSign: CGFloat { 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 { return CGFloat(Float.random) } /// Random CGFloat between 0 and n-1. /// /// - Parameter n: Interval max /// - Returns: Returns a random CGFloat point number between 0 and n max public static func random(min: CGFloat, max: CGFloat) -> CGFloat { return CGFloat.random * (max - min) + min } }
使用 :
let randomNumDouble = Double.random(min: 0.00, max: 23.50) let randomNumInt = Int.random(min: 56, max: 992) let randomNumFloat = Float.random(min: 6.98, max: 923.09) let randomNumCGFloat = CGFloat.random(min: 6.98, max: 923.09)
更新桑迪查普曼斯威夫特3 的答案 :
extension ClosedRange where Bound : FloatingPoint { public func random() -> Bound { let range = self.upperBound - self.lowerBound let randomValue = (Bound(arc4random_uniform(UINT32_MAX)) / Bound(UINT32_MAX)) * range + self.lowerBound return randomValue } }
现在你可以说像(-1.0...1.0).random()
这样的东西。
以下是IntervalType
types的扩展:
extension IntervalType { public func random() -> Bound { let range = (self.end as! Double) - (self.start as! Double) let randomValue = (Double(arc4random_uniform(UINT32_MAX)) / Double(UINT32_MAX)) * range + (self.start as! Double) return randomValue as! Bound } }
有了这个扩展,您可以使用间隔语法来生成一个间隔,然后在该间隔中获得一个随机值:
(0.0...1.0).random()
加成
如果您希望对Int
进行相同的操作,则可以在CollectionType
协议上使用以下扩展名:
extension CollectionType { public func random() -> Self._Element { if let startIndex = self.startIndex as? Int { let start = UInt32(startIndex) let end = UInt32(self.endIndex as! Int) return self[Int(arc4random_uniform(end - start) + start) as! Self.Index] } var generator = self.generate() var count = arc4random_uniform(UInt32(self.count as! Int)) while count > 0 { generator.next() count = count - 1 } return generator.next() as! Self._Element } }
Int
不使用IntervalType
。 他们使用Range
来代替。 在CollectionType
types上执行此操作的好处是,它会自动转到Dictionary
和Array
types。
例子:
(0...10).random() // Ex: 6 ["A", "B", "C"].random() // Ex: "B" ["X":1, "Y":2, "Z":3].random() // Ex: (.0: "Y", .1: 2)
这里框架在Swift中产生随机数字数据很好: https : //github.com/thellimist/SwiftRandom/blob/master/SwiftRandom/Randoms.swift
public extension Int { /// SwiftRandom extension public static func random(lower: Int = 0, _ upper: Int = 100) -> Int { return lower + Int(arc4random_uniform(UInt32(upper - lower + 1))) } } public extension Double { /// SwiftRandom extension public static func random(lower: Double = 0, _ upper: Double = 100) -> Double { return (Double(arc4random()) / 0xFFFFFFFF) * (upper - lower) + lower } } public extension Float { /// SwiftRandom extension public static func random(lower: Float = 0, _ upper: Float = 100) -> Float { return (Float(arc4random()) / 0xFFFFFFFF) * (upper - lower) + lower } } public extension CGFloat { /// SwiftRandom extension public static func random(lower: CGFloat = 0, _ upper: CGFloat = 1) -> CGFloat { return CGFloat(Float(arc4random()) / Float(UINT32_MAX)) * (upper - lower) + lower } }
jmduke提出的build议似乎在Playground中起作用:
func randomCGFloat() -> Float { return Float(arc4random()) / Float(UInt32.max) }
以及从swift文档和drewag所指出的原因:types转换必须是明确的,doc中的示例是:
let three = 3 let pointOneFourOneFiveNine = 0.14159 let pi = Double(three) + pointOneFourOneFiveNine // pi equals 3.14159, and is inferred to be of type Double