用法一
类型方法
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+
用法二
类型方法
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+
相关用法
- Swift Int random(in:using:)用法及代码示例
- Swift Int >>=(_:_:)用法及代码示例
- Swift Int &>>=(_:_:)用法及代码示例
- Swift Int *=(_:_:)用法及代码示例
- Swift Int ^(_:_:)用法及代码示例
- Swift Int &-(_:_:)用法及代码示例
- Swift Int init(exactly:)用法及代码示例
- Swift Int |(_:_:)用法及代码示例
- Swift Int &=(_:_:)用法及代码示例
- Swift Int <<(_:_:)用法及代码示例
- Swift Int +(_:)用法及代码示例
- Swift Int >>(_:_:)用法及代码示例
- Swift Int ..<(_:)用法及代码示例
- Swift Int /=(_:_:)用法及代码示例
- Swift Int multipliedFullWidth(by:)用法及代码示例
- Swift Int -(_:_:)用法及代码示例
- Swift Int ...(_:_:)用法及代码示例
- Swift Int &*=(_:_:)用法及代码示例
- Swift Int &(_:_:)用法及代码示例
- Swift Int /(_:_:)用法及代码示例
- Swift Int ...(_:)用法及代码示例
- Swift Int init(clamping:)用法及代码示例
- Swift Int ..<(_:_:)用法及代码示例
- Swift Int init(truncatingIfNeeded:)用法及代码示例
- Swift Int &*(_:_:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 Int random(in:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。