Created
December 29, 2015 23:54
-
-
Save mtrovilho/7e9775261ab0fd6ea999 to your computer and use it in GitHub Desktop.
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 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