Take the 2-minute tour ×

I have a NSMutableArray of N Integer elements (N>4), I want to get 3 different random elements from this array. I do not really need a perfectly-uniform distribution, just 3 different random elements should be OK. Do you have any suggestion? Thanks

share|improve this question

3 Answers 3

up vote 7 down vote accepted

Make NSIndexSet, and keep adding

int value = arc4random() % array.count;

items to it until its size gets to 3. The you know that you have your three indexes.

NSMutableIndexSet *picks = [NSMutableIndexSet indexSet];
do {
    [picks addIndex:arc4random() % array.count];
} while (picks.count != 3);
[picks enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
    NSLog(@"Element at index %ud: %@", idx, [array elementAt:idx]);
share|improve this answer
I do not really understand from the line [picks enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { NSLog(@"Element at index %ud: %@", idx, [array elementAt:idx]); }] . It just print out those 3 elements? –  DavidNg Sep 9 '12 at 1:04
@DavidNg Yes, this is just an illustration of how you can use the picks set. The block in my example prints the there elements and their indexes, but of course you are free to use them in any way you want. –  dasblinkenlight Sep 9 '12 at 1:12
    int index = (int)(arc4random() % [array count]);
    id object = [array objectAtIndex:index];
    [array removeObjectAtIndex:index];

arc4random() returns a random number in the range [0,2^32-1). The remainder when you take the modulus with the size of the array gets you a value between [0,arrayCountLessOne].

If you don't want to change your original data array, you can just make a copy of the array.

share|improve this answer
Thanks, I was indeed thinking of rand(). Fixed! –  FeifanZ Sep 9 '12 at 1:00

If you want to do this more than once from various places in your code then consider doing this: The "Objective C way" is to create a category on NSMutableArray which adds a method randomObjects. The method itself should generate three random integers from 0 to the length of the array -1 (N-1), then return a set of objects from the array at those indices, as per the other answers here (dasblinkenlight's in particular.)

First, create the category. Create a new header file NSMutableArray+RandomObject.h, containing:

@interface NSMutableArray (RandomObjects)

- (NSSet *) randomObjects;


The RandomElement in parentheses is the name of your category. Any class you write that includes this new header file will give all your NSMutableArray instances the randomElement method.

Then the implementation, in NSMutableArray+RandomObjects.m:

@implementation NSMutableArray (RandomObjects)

- (NSSet *) randomObjects {
  // Use the code from @dasblinkenlight's answer here, adding the following line:
  return picks;


And that's basically it. You've effectively added that capability to NSMutableArray.

share|improve this answer
I would only refactor it into a category when I need to call such a method often enough. If its only used once, a category is way too much overkill for this. –  JustSid Sep 9 '12 at 1:00
@JustSid I completely agree. Will add a note to that effect. –  Tim Kemp Sep 9 '12 at 1:01

Your Answer


