<分区>
我试图生成介于 0 和 1 之间的随机 double ,但我得到的总是 0
func randFunct() -> Double {
let randomInt = arc4random_uniform(100) + 1 // [1, 100]
let randomDouble : Double = Double(randomInt/100)
return randomDouble
}
<分区>
我试图生成介于 0 和 1 之间的随机 double ,但我得到的总是 0
func randFunct() -> Double {
let randomInt = arc4random_uniform(100) + 1 // [1, 100]
let randomDouble : Double = Double(randomInt/100)
return randomDouble
}
最佳答案
试试这个:
extension Double {
private static let arc4randomMax = Double(UInt32.max)
static func random0to1() -> Double {
return Double(arc4random()) / arc4randomMax
}
}
print(Double.random0to1())
关于ios - 0到1之间的swift随机双数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39172136/
相关文章:
ios - 如何通过 iOS 应用程序中的 UIPickerView 实现 View 之间的转换?
ios - 为什么在某些情况下 View 的快照看起来会大两倍?
ios - EKEvent 以编程方式创建,从 iPhone 日历中获取 EKEvent,识别两者 (EKEvents)
ios - 无法将类型 'IndexPath.Type' 的值转换为预期的参数类型 'IndexPath'
ios - 如何删除警告 "Conditional cast from ' 任何'到 'AnyObject' 总是成功"
ios - textFieldShouldReturn - completionHandler 中的一些方法永远不会被调用