Xcode 11 ? Swift 5.1 or later
extension Range where Bound: FixedWidthInteger {
var random: Bound { .random(in: self) }
func random(_ n: Int) -> [Bound] { (0..<n).map { _ in random } }
}
extension ClosedRange where Bound: FixedWidthInteger {
var random: Bound { .random(in: self) }
func random(_ n: Int) -> [Bound] { (0..<n).map { _ in random } }
}
Note: For older Swift versions check the edit history
Usage:
(10...20).random // 16
(0...1).random(10) // [0, 1, 0, 0, 1, 1, 1, 1, 1, 0]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…