Skip to content

Instantly share code, notes, and snippets.

@mtrovilho
Created December 29, 2015 23:54
Show Gist options
  • Save mtrovilho/7e9775261ab0fd6ea999 to your computer and use it in GitHub Desktop.
Save mtrovilho/7e9775261ab0fd6ea999 to your computer and use it in GitHub Desktop.
extension Array {
func shuffle() -> [Element] {
var shuffledCollection = self
var remainingElements = self.count
while remainingElements > 0 {
let currentIndex = remainingElements - 1
let newIndex = Int(arc4random_uniform(UInt32(remainingElements)))
let temp = shuffledCollection[currentIndex]
shuffledCollection[currentIndex] = shuffledCollection[newIndex]
shuffledCollection[newIndex] = temp
remainingElements -= 1
}
return shuffledCollection
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment