Created
March 23, 2017 13:10
-
-
Save qmihara/63ab8a7205f76769232f284e11823c68 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
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