Skip to content

Instantly share code, notes, and snippets.

@haldun
Created December 13, 2016 12:00
Show Gist options
  • Save haldun/9885b6c98cf0fe9575b46de8a710c95b to your computer and use it in GitHub Desktop.
Save haldun/9885b6c98cf0fe9575b46de8a710c95b to your computer and use it in GitHub Desktop.
Random for collections
extension RandomAccessCollection {
func random() -> Iterator.Element? {
guard count > 0 else { return nil }
let offset = arc4random_uniform(numericCast(count))
let i = index(startIndex, offsetBy: numericCast(offset))
return self[i]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment