Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a random object from NSArray without duplication

I have an NSArray with 17 objects, something like this:

NSArray *objArray = [[NSArray alloc]initWithObjects: @"1",@"2",@"3",@"4",@"5",@"6"
,@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17", nil];

and an int with a random number as follows:

int random = arc4random()%17+1;

I want to get a random object from this NSArray without it being a duplicate, even if I closed the app (maybe by using NSUserDefaults).

If I've gotten all the objects I want to generate a new random sequence for the same objects.

like image 727
Mutawe Avatar asked May 28 '13 11:05

Mutawe


2 Answers

You could do this by making a mutable copy of the array, and after you make a random selection from that array, remove that same object. When you want to save the array, save the mutable array itself, so can resume where you left off when the app restarts. This little test app does that, and just logs the value of the random pick:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.objArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17"];
    self.mut = [self.objArray mutableCopy];
}

-(IBAction)pickNumber:(id)sender {
    int index = arc4random_uniform(self.mut.count);
     NSLog(@"%@", self.mut[index]);
    [self.mut removeObjectAtIndex:index];
    if (self.mut.count == 0) {
        self.mut = [self.objArray mutableCopy];
        NSLog(@"*******************");
    }
}
like image 173
rdelmar Avatar answered Oct 03 '22 16:10

rdelmar


As a starting point, you could shuffle your array:

+ (NSArray *)arrayByShufflingArray:(NSArray *)array
{
    // Fisher-Yates algorithm
    NSMutableArray *result = [array mutableCopy];
    NSUInteger count = [result count];
    for (NSInteger i = ((NSInteger) count) - 1; i > 0; i--) {
        NSUInteger firstIndex = (NSUInteger)i;
        NSUInteger secondIndex = arc4random() % (NSUInteger)(i + 1);

        [result exchangeObjectAtIndex:firstIndex withObjectAtIndex:secondIndex];
    }

    return result;
}

Step through each shuffled element and when you get to the end, reshuffle.

It can still happen that an item is selected twice in a row when the last item of one shuffle is the same as the first item in the next shuffle. If you want to avoid this you'll have to add some additional code.

like image 45
2 revs Avatar answered Oct 03 '22 15:10

2 revs