Last active
June 7, 2019 03:19
-
-
Save seanwoodward/1d6e38752b91eb5bb8b68a9886e03b47 to your computer and use it in GitHub Desktop.
Pick a number of random elements from an array. Better way?
This file contains hidden or 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 Collection { | |
func random(pick: Int) -> [Element] { | |
return self.indices.shuffled().prefix(pick).map { self[$0] } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment