用法一
類型方法
random(in:)
返回指定範圍內的隨機值。
聲明
static func random(in range: ClosedRange<Self>) -> Self
當
RawSignificand
符合 FixedWidthInteger
時可用。返回值
range
範圍內的隨機值。
參數
range
創建隨機值的範圍。必須是有限的。
詳述
使用此方法生成特定範圍內的浮點值。此示例在 10.0 ... 20.0
範圍內創建三個新值。
for _ in 1...3 {
print(Double.random(in: 10.0 ... 20.0))
}
// Prints "18.1900709259179"
// Prints "14.2286325689993"
// Prints "13.1485686260762"
random()
靜態方法從 range
中的連續均勻分布中選擇一個隨機值,然後將該值轉換為該類型中最接近的可表示值。根據 range
的大小和跨度,某些具體值可能比其他值更頻繁地表示。
該方法相當於調用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
當
RawSignificand
符合 FixedWidthInteger
時可用。返回值
range
範圍內的隨機值。
參數
range
創建隨機值的範圍。
range
必須是有限且非空的。
詳述
使用此方法生成特定範圍內的浮點值。此示例在 10.0 ..< 20.0
範圍內創建三個新值。
for _ in 1...3 {
print(Double.random(in: 10.0 ..< 20.0))
}
// Prints "18.1900709259179"
// Prints "14.2286325689993"
// Prints "13.1485686260762"
random()
靜態方法從 range
中的連續均勻分布中選擇一個隨機值,然後將該值轉換為該類型中最接近的可表示值。根據 range
的大小和跨度,某些具體值可能比其他值更頻繁地表示。
該方法相當於調用random(in:using:)
,傳入係統默認的隨機生成器。
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift BinaryFloatingPoint random(in:using:)用法及代碼示例
- Swift BinaryFloatingPoint binade用法及代碼示例
- Swift BinaryFloatingPoint exponentBitCount用法及代碼示例
- Swift BinaryInteger quotientAndRemainder(dividingBy:)用法及代碼示例
- Swift BinaryInteger trailingZeroBitCount用法及代碼示例
- Swift BinaryInteger init(clamping:)用法及代碼示例
- Swift BinaryInteger <<=(_:_:)用法及代碼示例
- Swift BinaryInteger !=(_:_:)用法及代碼示例
- Swift BinaryInteger /(_:_:)用法及代碼示例
- Swift BinaryInteger init(truncatingIfNeeded:)用法及代碼示例
- Swift BinaryInteger ==(_:_:)用法及代碼示例
- Swift BinaryInteger init(_:)用法及代碼示例
- Swift BinaryInteger %=(_:_:)用法及代碼示例
- Swift BinaryInteger &=(_:_:)用法及代碼示例
- Swift BinaryInteger |(_:_:)用法及代碼示例
- Swift BinaryInteger +(_:_:)用法及代碼示例
- Swift BinaryInteger &(_:_:)用法及代碼示例
- Swift BinaryInteger ^=(_:_:)用法及代碼示例
- Swift BinaryInteger ^(_:_:)用法及代碼示例
- Swift BinaryInteger %(_:_:)用法及代碼示例
- Swift BinaryInteger *(_:_:)用法及代碼示例
- Swift BinaryInteger -(_:_:)用法及代碼示例
- Swift BinaryInteger /=(_:_:)用法及代碼示例
- Swift BinaryInteger >>=(_:_:)用法及代碼示例
- Swift BinaryInteger ~(_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 BinaryFloatingPoint random(in:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。