用法一
类型方法
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 FixedWidthInteger random(in:)用法及代码示例
- Swift FixedWidthInteger &-=(_:_:)用法及代码示例
- Swift FixedWidthInteger &<<=(_:_:)用法及代码示例
- Swift FixedWidthInteger &<<(_:_:)用法及代码示例
- Swift FixedWidthInteger multipliedFullWidth(by:)用法及代码示例
- Swift FixedWidthInteger &-(_:_:)用法及代码示例
- Swift FixedWidthInteger &*=(_:_:)用法及代码示例
- Swift FixedWidthInteger nonzeroBitCount用法及代码示例
- Swift FixedWidthInteger init(_:radix:)用法及代码示例
- Swift FixedWidthInteger &>>=(_:_:)用法及代码示例
- Swift FixedWidthInteger dividingFullWidth(_:)用法及代码示例
- Swift FixedWidthInteger leadingZeroBitCount用法及代码示例
- Swift FixedWidthInteger &>>(_:_:)用法及代码示例
- Swift FixedWidthInteger &+=(_:_:)用法及代码示例
- Swift FixedWidthInteger &*(_:_:)用法及代码示例
- Swift FixedWidthInteger &+(_:_:)用法及代码示例
- Swift FixedWidthInteger用法及代码示例
- Swift FlattenSequence prefix(_:)用法及代码示例
- Swift Float -(_:)用法及代码示例
- Swift FlattenSequence max(by:)用法及代码示例
- Swift Float80 /(_:_:)用法及代码示例
- Swift Float ...(_:_:)用法及代码示例
- Swift FloatingPointRoundingRule.toNearestOrAwayFromZero用法及代码示例
- Swift Float16 ...(_:_:)用法及代码示例
- Swift FloatingPointRoundingRule.up用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 FixedWidthInteger random(in:using:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。