Skip to content

Instantly share code, notes, and snippets.

@edc0der
Created April 10, 2018 00:55
Show Gist options
  • Save edc0der/650fc59a8dba1cf0fbdec7b0988a3690 to your computer and use it in GitHub Desktop.
Save edc0der/650fc59a8dba1cf0fbdec7b0988a3690 to your computer and use it in GitHub Desktop.
Shuffle an array in Swift
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))))
}
}
@edc0der
Copy link
Author

edc0der commented Apr 10, 2018

Based on Fisher-Yates shuffling algorithm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment