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