bogdan barbulescu bogdan barbulescu - 3 months ago 12
Swift Question

Generate number of 9 digits in Swift

How can I generate a 9 digits number in Swift.
I have tried random() function, but the size of the generated number is different every time the function is called.
I have also tried this code enter link description here , but it is deprecated.

Answer
import Foundation

func random9DigitString() -> String {
    let min: UInt32 = 100_000_000
    let max: UInt32 = 999_999_999
    let i = min + arc4random_uniform(max - min + 1)
    return String(i)
}
Comments