Skip to content

Instantly share code, notes, and snippets.

@mkhl
Created November 10, 2015 09:49
Show Gist options
  • Save mkhl/6cd385b573757c49587b to your computer and use it in GitHub Desktop.
Save mkhl/6cd385b573757c49587b to your computer and use it in GitHub Desktop.
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