Created
November 10, 2015 09:49
-
-
Save mkhl/6cd385b573757c49587b to your computer and use it in GitHub Desktop.
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 Array { | |
mutating func shuffle() { | |
if self.count < 2 { return } | |
for i in 0..<self.count { | |
let j = Int(arc4random_uniform(UInt32(self.count - i))) + i | |
if i != j { | |
swap(&self[i], &self[j]) | |
} | |
} | |
} | |
func shuffled() -> [Element] { | |
if self.count < 2 { return self } | |
var copy = self | |
copy.shuffle() | |
return copy | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment