Skip to content

Instantly share code, notes, and snippets.

@saiday
Created March 6, 2015 13:24
Show Gist options
  • Select an option

  • Save saiday/f323d65a7ba9897fc303 to your computer and use it in GitHub Desktop.

Select an option

Save saiday/f323d65a7ba9897fc303 to your computer and use it in GitHub Desktop.
NSArray shuffle items
- (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