var fourDigitNumber: String {
var result = ""
repeat {
// Create a string with a random number 0...9999
result = String(format:"%04d", arc4random_uniform(10000) )
} while Set<Character>(result.characters).count < 4
return result
}
// USAGE
let password = fourDigitNumber
print("Generated Password : \(password)")
Last active
March 3, 2020 07:47
-
-
Save WrathChaos/0c1cf35065b3b9730f8d7376dc6ac65a to your computer and use it in GitHub Desktop.
How to generate 4-digit random number with Swift? Article: https://freakycoder.com/ios-notes-41-how-to-generate-4-digit-random-number-in-swift-3-31194c87f4b4
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Set<Character>(result.characters).count
is deprecated useresult.count