Created
March 6, 2015 13:24
-
-
Save saiday/f323d65a7ba9897fc303 to your computer and use it in GitHub Desktop.
NSArray shuffle items
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
| - (NSArray *)shuffle | |
| { | |
| NSUInteger count = [self count]; | |
| NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:self]; | |
| for (NSUInteger i = 0; i < count; ++i) { | |
| NSInteger remainingCount = count - i; | |
| NSInteger exchangeIndex = i + arc4random_uniform((u_int32_t )remainingCount); | |
| [mutableArray exchangeObjectAtIndex:i withObjectAtIndex:exchangeIndex]; | |
| } | |
| return mutableArray; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment