用法一
类型方法
random(in:
random(in:using:)
使用给定的生成器作为随机源,返回指定范围内的随机值。
声明
static func random<T>(
in range: Range<Self>,
using generator: inout T
) -> Self where T : RandomNumberGenerator
返回值
range
范围内的随机值。
参数
range
创建随机值的范围。
range
不能为空。generator
创建新随机值时使用的随机数生成器。
详述
当您使用自定义随机数生成器时,使用此方法生成特定范围内的整数。此示例在 1..<100
范围内创建三个新值。
for _ in 1...3 {
print(Int.random(in: 1..<100, using: &myGenerator))
}
// Prints "7"
// Prints "44"
// Prints "21"
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
用法二
类型方法
random(in:
random(in:using:)
使用给定的生成器作为随机源,返回指定范围内的随机值。
声明
static func random<T>(
in range: ClosedRange<Self>,
using generator: inout T
) -> Self where T : RandomNumberGenerator
返回值
range
范围内的随机值。
参数
range
创建随机值的范围。
generator
创建新随机值时使用的随机数生成器。
详述
当您使用自定义随机数生成器时,使用此方法生成特定范围内的整数。此示例在 1...100
范围内创建三个新值。
for _ in 1...3 {
print(Int.random(in: 1...100, using: &myGenerator))
}
// Prints "7"
// Prints "44"
// Prints "21"
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相关用法
- Swift Int32 random(in:)用法及代码示例
- Swift Int32 <<=(_:_:)用法及代码示例
- Swift Int32 &=(_:_:)用法及代码示例
- Swift Int32 &<<=(_:_:)用法及代码示例
- Swift Int32 ...(_:_:)用法及代码示例
- Swift Int32 negate()用法及代码示例
- Swift Int32 multipliedFullWidth(by:)用法及代码示例
- Swift Int32 init(integerLiteral:)用法及代码示例
- Swift Int32 &(_:_:)用法及代码示例
- Swift Int32 init(truncatingIfNeeded:)用法及代码示例
- Swift Int32 +=(_:_:)用法及代码示例
- Swift Int32 init(_:radix:)用法及代码示例
- Swift Int32 &+(_:_:)用法及代码示例
- Swift Int32 !=(_:_:)用法及代码示例
- Swift Int32 -(_:)用法及代码示例
- Swift Int32 init(clamping:)用法及代码示例
- Swift Int32 ..<(_:_:)用法及代码示例
- Swift Int32 -=(_:_:)用法及代码示例
- Swift Int32 %(_:_:)用法及代码示例
- Swift Int32 <<(_:_:)用法及代码示例
- Swift Int32 init(_:)用法及代码示例
- Swift Int32 trailingZeroBitCount用法及代码示例
- Swift Int32 >>(_:_:)用法及代码示例
- Swift Int32 &>>(_:_:)用法及代码示例
- Swift Int32 -(_:_:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 Int32 random(in:using:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。