Skip to content

Instantly share code, notes, and snippets.

@gingofthesouth
Last active August 23, 2016 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gingofthesouth/54bea667b28a815b2fe33a4da986e327 to your computer and use it in GitHub Desktop.
Save gingofthesouth/54bea667b28a815b2fe33a4da986e327 to your computer and use it in GitHub Desktop.
Swift 2.2 Random String Generation
// based on https://gist.github.com/samuel-mellert/20b3c99dec168255a046
// which is based on https://gist.github.com/szhernovoy/276e69eb90a0de84dd90
// Updated to work on Swift 2.2
func randomString(length: Int) -> String {
let charactersString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let charactersArray : [Character] = Array(charactersString.characters)
var string = ""
for _ in 0..<length {
string.append(charactersArray[Int(arc4random()) % charactersArray.count])
}
return string
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment