Created
April 10, 2018 00:55
-
-
Save edc0der/650fc59a8dba1cf0fbdec7b0988a3690 to your computer and use it in GitHub Desktop.
Shuffle an array in Swift
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
func randomizeArray<T>(_ arr: inout Array<T>) -> Void { | |
let maxIndex = arr.count - 1 | |
for i in 0...maxIndex { | |
arr.swapAt(i, Int(arc4random_uniform(UInt32(maxIndex + 1)))) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on Fisher-Yates shuffling algorithm