5

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.

jscs
  • 63,694
  • 13
  • 151
  • 195
Mutawe
  • 6,464
  • 3
  • 47
  • 90

3 Answers3

7

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(@"*******************");
    }
}
rdelmar
  • 103,982
  • 12
  • 207
  • 218
3

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.

Mike Weller
  • 45,401
  • 15
  • 131
  • 151
  • TI tried this idea, but i had duplicated objects called, BTW one of my objects "XXX" been called 3 times in a row with this methodology – Mutawe May 28 '13 at 12:01
  • @Mutawe if you shuffle the array, you can then go through the objects in order, no need to randomly select. – Greg May 28 '13 at 12:05
  • Now i got a shuffled array, but what can i do with it, store it in NSUserDefaults??, store the count from 1 - 17, and if count == 17 re shuffle it again ?? – Mutawe May 28 '13 at 12:22
-1

Just Copy and Paste

-(NSMutableArray*)getRandomValueFromArray:(NSMutableArray*)arrAllData randomDataCount:(NSInteger)count {  
 NSMutableArray *arrFilterData = [[NSMutableArray alloc]init];
for(int i=0; i<count; i++){

     NSInteger index = arc4random() % (NSUInteger)(arrAllData.count);
    [arrFilterData addObject:[arrAllData objectAtIndex:index]];
    [arrAllData removeObjectAtIndex:index];
}
return arrFilterData;
}

Note: count = number of random values you want to fetch from array.

ZygD
  • 22,092
  • 39
  • 79
  • 102
Alok
  • 24,880
  • 6
  • 40
  • 67