类型方法
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:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。