Skip to content

Instantly share code, notes, and snippets.

@MartinJNash
Created April 26, 2015 10:00
Show Gist options
  • Save MartinJNash/c326035565e4459993bd to your computer and use it in GitHub Desktop.
Save MartinJNash/c326035565e4459993bd to your computer and use it in GitHub Desktop.
extension Array {
var shuffled: Array<T> {
var newer = self
newer.shuffle()
return newer
}
mutating func shuffle() {
let count = self.count
if count > 1 {
for x in 0..<count {
let idx = Int(arc4random_uniform(UInt32(x+1)))
swap(&self[x], &self[idx])
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment