用法一
类型方法
random(in:)
返回指定范围内的随机值。
声明
static func random(in range: Range<Self>) -> Self
返回值
range
范围内的随机值。
参数
range
创建随机值的范围。
range
不能为空。
详述
使用此方法生成特定范围内的整数。此示例在 1..<100
范围内创建三个新值。
for _ in 1...3 {
print(Int.random(in: 1..<100))
}
// Prints "53"
// Prints "64"
// Prints "5"
该方法相当于调用带生成器的版本,传入系统默认的随机生成器。
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
用法二
类型方法
random(in:)
返回指定范围内的随机值。
声明
static func random(in range: ClosedRange<Self>) -> Self
返回值
range
范围内的随机值。
参数
range
创建随机值的范围。
详述
使用此方法生成特定范围内的整数。此示例在 1...100
范围内创建三个新值。
for _ in 1...3 {
print(Int.random(in: 1...100))
}
// Prints "53"
// Prints "64"
// Prints "5"
该方法相当于调用random(in:using:)
,传入系统默认的随机生成器。
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相关用法
- Swift FixedWidthInteger random(in:using:)用法及代码示例
- 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:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。