-
-
Save robertmryan/e11a9fd983a115074b8e to your computer and use it in GitHub Desktop.
Fisher-Yates shuffle of `NSMutableArray` in Swift
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
/// 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