Last active
April 14, 2016 21:24
-
-
Save buddax2/3e1c205b8901c901597d50e1e263d7b0 to your computer and use it in GitHub Desktop.
Returns shuffled array
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 { | |
func shuffled() -> [Element] { | |
return shuffle(self) | |
} | |
func shuffle(array: [Element]) -> [Element] { | |
guard array.count > 1 else { return array } | |
var list = array | |
let randomIndex = Int(arc4random_uniform(UInt32(array.count))) | |
return [list.removeAtIndex(randomIndex)] + shuffle(list) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment