I got an NSMutableArray with a rather small number of objects that varies from 1 to max 20.
When user taps a button I want to select one random object and display it. I've tried shuffling the array:
srandom(time(NULL));
for (NSInteger x = 0; x < [array count]; x++) {
NSInteger randInt = (random() % ([array count] - x)) + x;
[array exchangeObjectAtIndex:x withObjectAtIndex:randInt];
}
And then select a random index using arc4random() but I get many repetitions. I do not want to removeObjectAtIndex as this would only reduce my array to zero and then crash. Is there a way to repopulate the array with the same objects once reached zero and start over again?
Thanks
I managed to "solve" my issue by doing the following: It maybe messy but I would like some input on this.