用法一
類型方法
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 Int64 random(in:using:)用法及代碼示例
- Swift Int64 -(_:)用法及代碼示例
- Swift Int64 leadingZeroBitCount用法及代碼示例
- Swift Int64 quotientAndRemainder(dividingBy:)用法及代碼示例
- Swift Int64 !=(_:_:)用法及代碼示例
- Swift Int64 &=(_:_:)用法及代碼示例
- Swift Int64 nonzeroBitCount用法及代碼示例
- Swift Int64 -=(_:_:)用法及代碼示例
- Swift Int64 ~(_:)用法及代碼示例
- Swift Int64 <<(_:_:)用法及代碼示例
- Swift Int64 init(integerLiteral:)用法及代碼示例
- Swift Int64 multipliedFullWidth(by:)用法及代碼示例
- Swift Int64 -(_:_:)用法及代碼示例
- Swift Int64 trailingZeroBitCount用法及代碼示例
- Swift Int64 ...(_:)用法及代碼示例
- Swift Int64 init(truncatingIfNeeded:)用法及代碼示例
- Swift Int64 %(_:_:)用法及代碼示例
- Swift Int64 &-(_:_:)用法及代碼示例
- Swift Int64 ..<(_:_:)用法及代碼示例
- Swift Int64 /(_:_:)用法及代碼示例
- Swift Int64 /=(_:_:)用法及代碼示例
- Swift Int64 init(_:)用法及代碼示例
- Swift Int64 ^(_:_:)用法及代碼示例
- Swift Int64 &*(_:_:)用法及代碼示例
- Swift Int64 &+=(_:_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Int64 random(in:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。