jeudi 14 janvier 2016

Issue with arc4random implementation

I'm trying to get a random number from an array using arc4random. I have tried everything I can think of but I just can't get it to work as every time it just returns 81 which is the number of items in the array. I just need it to select a random number from however many items are in the array. This is my code:

 _equations = @[@"", @"", @"", @"", @"", @"", @"", @"", @"9", @"", @"", @"", @"", @"", @"", @"", @"", @"18", @"", @"", @"", @"", @"", @"", @"", @"", @"27", @"", @"", @"", @"", @"", @"", @"", @"", @"36", @"", @"", @"", @"", @"", @"", @"", @"", @"45", @"", @"", @"", @"", @"", @"", @"", @"", @"54", @"", @"", @"", @"", @"", @"", @"", @"", @"63", @"", @"", @"", @"", @"", @"", @"", @"", @"72", @"", @"", @"", @"", @"", @"", @"", @"" ,@"81" ];
 NSUInteger randomEquationNo = arc4random() % [_equations count];
 int index = arc4random_uniform(81);
 id randomNo = nil;
 if ([_equations count] > 0){
    int index = arc4random()%[_equations count];
    randomNo = [_equations objectAtIndex:index];
 }

What am I doing wrong and how should I fix it?

Thanks




Aucun commentaire:

Enregistrer un commentaire