Created
May 4, 2016 07:22
-
-
Save anjerodesu/7cf82c51d0d7dacffa13cd4100000ac3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
static func random(length: Int) -> String { | |
let base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | |
var randomString: String = "" | |
for _ in 0..<length { | |
let randomValue = Int.random(from: 0, to: base.characters.count) | |
randomString.append(base[base.startIndex.advancedBy(randomValue)]) | |
} | |
return randomString | |
} | |
} | |
extension Int { | |
static func random(from from: Int, to: Int) -> Int { | |
return from + (Int(arc4random()) % (to - from)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment