Last active
September 2, 2016 07:01
-
-
Save dfelber/bde6daf491657f9b8b6e7bcd72142c33 to your computer and use it in GitHub Desktop.
Returns a random value from an array
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 Array { | |
func randomValue() -> Element? { | |
guard self.isEmpty == false else { | |
return nil | |
} | |
let idx = Int(arc4random() % UInt32(count)) | |
return self[idx] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment