How to generate a random number in a range (10...20) using Swift [duplicate]

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]