Created
December 13, 2016 12:00
-
-
Save haldun/9885b6c98cf0fe9575b46de8a710c95b to your computer and use it in GitHub Desktop.
Random for collections
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 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