類型方法
random(using:)
返回一個隨機布爾值,使用給定的生成器作為隨機源。
聲明
static func random<T>(using generator: inout T) -> Bool where T : RandomNumberGenerator
返回值
true
或 false
以相等的概率隨機選擇。
參數
generator
創建新隨機值時使用的隨機數生成器。
詳述
此方法以相等的概率返回true
和false
。當您使用自定義隨機數生成器時,使用此方法生成隨機布爾值。
let flippedHeads = Bool.random(using: &myGenerator)
if flippedHeads {
print("Heads, you win!")
} else {
print("Maybe another try?")
}
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift Bool random()用法及代碼示例
- Swift Bool !(_:)用法及代碼示例
- Swift Bool ||(_:_:)用法及代碼示例
- Swift Bool init(booleanLiteral:)用法及代碼示例
- Swift Bool &&(_:_:)用法及代碼示例
- Swift Bool toggle()用法及代碼示例
- Swift Bool用法及代碼示例
- Swift BooleanLiteralType用法及代碼示例
- Swift BinaryInteger quotientAndRemainder(dividingBy:)用法及代碼示例
- Swift BinaryInteger trailingZeroBitCount用法及代碼示例
- Swift BinaryInteger init(clamping:)用法及代碼示例
- Swift BinaryInteger <<=(_:_:)用法及代碼示例
- Swift BinaryInteger !=(_:_:)用法及代碼示例
- Swift BinaryInteger /(_:_:)用法及代碼示例
- Swift BinaryInteger init(truncatingIfNeeded:)用法及代碼示例
- Swift BinaryInteger ==(_:_:)用法及代碼示例
- Swift BinaryFloatingPoint binade用法及代碼示例
- Swift BidirectionalCollection subscript(_:)用法及代碼示例
- Swift BidirectionalCollection last(where:)用法及代碼示例
- Swift BidirectionalCollection lastIndex(where:)用法及代碼示例
- Swift BinaryInteger init(_:)用法及代碼示例
- Swift BinaryInteger %=(_:_:)用法及代碼示例
- Swift BinaryFloatingPoint random(in:using:)用法及代碼示例
- Swift BinaryInteger &=(_:_:)用法及代碼示例
- Swift BidirectionalCollection lastIndex(of:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Bool random(using:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。