Skip to content

Instantly share code, notes, and snippets.

@solkar
Created February 4, 2014 06:50
Show Gist options
  • Save solkar/8799171 to your computer and use it in GitHub Desktop.
Save solkar/8799171 to your computer and use it in GitHub Desktop.
shuffle NSMutableArray category
@interface NSMutableArray (Shuffling)
- (void)shuffle;
@end
#import "NSMutableArray+Shuffling.h"
@implementation NSMutableArray (Shuffling)
- (void)shuffle
{
NSUInteger count = [self count];
for (NSUInteger i = 0; i < count; ++i) {
// Select a random element between i and end of array to swap with.
NSInteger nElements = count - i;
NSInteger n = arc4random_uniform(nElements) + i;
[self exchangeObjectAtIndex:i withObjectAtIndex:n];
}
}
@end
@solkar
Copy link
Author

solkar commented Feb 4, 2014

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