NSArray category for shuffling arrays.
bjl_shuffledArray
- (NSArray *)bjl_shuffledArray
Return Value
Returns an array where all elements are shuffled into random order.
bjl_shuffledArrayWithItemLimit:
- (NSArray *)bjl_shuffledArrayWithItemLimit:(NSUInteger)itemLimit
Parameters
itemLimit
The number of elements to randomly select from the array.
Return Value
Returns an array with a limited number of random elements.
Discussion
This method will improve performance if you only need a few elements out of a large dataset.
Add the NSArray+Shuffle.h
and NSArray+Shuffle.m
files to your Xcode project.
This category implements the Fisher-Yates shuffle algorithm and uses the arc4random_uniform random number generator.
Helpful reference articles on array shuffling:
Fisher–Yates shuffle - Wikipedia, the free encyclopedia
Coding Horror: The Danger of Naïveté
Eli Bendersky's website » The intuition behind Fisher-Yates shuffling