Skip to content

Instantly share code, notes, and snippets.

@seanwoodward
Last active June 7, 2019 03:19
Show Gist options
  • Save seanwoodward/1d6e38752b91eb5bb8b68a9886e03b47 to your computer and use it in GitHub Desktop.
Save seanwoodward/1d6e38752b91eb5bb8b68a9886e03b47 to your computer and use it in GitHub Desktop.
Pick a number of random elements from an array. Better way?
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