Created
April 26, 2015 10:00
-
-
Save MartinJNash/c326035565e4459993bd 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 { | |
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