Skip to content

Instantly share code, notes, and snippets.

@robertmryan
Created March 22, 2015 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertmryan/e11a9fd983a115074b8e to your computer and use it in GitHub Desktop.
Save robertmryan/e11a9fd983a115074b8e to your computer and use it in GitHub Desktop.
Fisher-Yates shuffle of `NSMutableArray` in Swift
/// Shuffle NSMutableArray
///
/// Applies Fisher-Yates to ensure even distribution.
/// See http://en.wikipedia.org/wiki/Fisher–Yates_shuffle
func shuffle(array: NSMutableArray) {
let count = array.count
for i in 0 ..< (count - 1) {
let j = Int(arc4random_uniform(UInt32(count - i))) + i
array.exchangeObjectAtIndex(i, withObjectAtIndex: j)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment