Skip to content

Instantly share code, notes, and snippets.

@qmihara
Created March 23, 2017 13:10
Show Gist options
  • Save qmihara/63ab8a7205f76769232f284e11823c68 to your computer and use it in GitHub Desktop.
Save qmihara/63ab8a7205f76769232f284e11823c68 to your computer and use it in GitHub Desktop.
import Foundation
extension Array {
func shuffled() -> [Element] {
var array = self
(0..<count).reversed().forEach { i in
let j = Int(arc4random_uniform(UInt32(i + 1)))
let tmp = array[i]
array[i] = array[j]
array[j] = tmp
}
return array
}
}
let friends = ["serval", "wolf", "python", "cheetah", "brown bear"]
friends.shuffled()[0..<2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment