Created
March 26, 2016 08:24
-
-
Save akio0911/13727a5d4c36d2217243 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 CollectionType { | |
func shuffled() -> [Generator.Element] { | |
var source = Array(self) | |
var result : [Generator.Element] = [] | |
while !source.isEmpty { | |
let index = Int(arc4random()) % source.count | |
result.append(source[index]) | |
source.removeAtIndex(index) | |
} | |
return result | |
} | |
} | |
[1,2,3,4].shuffled() | |
["key1":1, "key2":2, "key3":3].shuffled() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment