Skip to content

Instantly share code, notes, and snippets.

@akio0911
Created March 26, 2016 08:24
Show Gist options
  • Save akio0911/13727a5d4c36d2217243 to your computer and use it in GitHub Desktop.
Save akio0911/13727a5d4c36d2217243 to your computer and use it in GitHub Desktop.
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