Skip to content

Instantly share code, notes, and snippets.

@dfelber
Last active September 2, 2016 07:01
Show Gist options
  • Save dfelber/bde6daf491657f9b8b6e7bcd72142c33 to your computer and use it in GitHub Desktop.
Save dfelber/bde6daf491657f9b8b6e7bcd72142c33 to your computer and use it in GitHub Desktop.
Returns a random value from an array
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