Getting a random object from NSArray without duplication

醉酒当歌 提交于 2019-11-26 09:13:45

问题


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.


回答1:


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(@"*******************");
    }
}



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/16791343/getting-a-random-object-from-nsarray-without-duplication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!