Search code examples
iosobjective-carc4random

Objective-C: Randomizer objectAtIndex Not sorting through All Types


So I have a "food" table that has three types: "Meals, Desserts, and Snacks". I have these categories attached to buttons in a Search Options page and they basically do a "Select * from food where type = 'Meals'" kind of thing and that works fine.

The problem is that when I try to randomly retrieve an object from "All" ("select * from food") it only returns meals. I'm thinking it's a problem maybe with the "objectAtIndex:0"?

ranDom = [_entries bjl_shuffledArray];
        Place *p = [ranDom objectAtIndex:0];

So I tried to randomize the index as well:

NSInteger randomIndex = arc4random()%[_entries count];
        ranDom = [_entries bjl_shuffledArray];
        Place *p = [ranDom objectAtIndex:randomIndex];

...but it still seems to only be retrieving results from Meals... not the other two types in "food".

Any idea how I can get it to better return results from all three types?

EDIT: Here's how bjl_shuffled_array works:

- (NSArray *)bjl_shuffledArray
{
    NSMutableArray *shuffledArray = [self mutableCopy];
    NSUInteger arrayCount = [shuffledArray count];

     if (arrayCount > 0) {
    for (NSUInteger i = arrayCount - 1; i > 0; i--) {
        NSUInteger n = arc4random_uniform(i + 1);
        [shuffledArray exchangeObjectAtIndex:i withObjectAtIndex:n];
    }
     }
    return [shuffledArray copy];
}

Solution

  • So the "_entries" was only returning 30 results. I knew there had to be a limit somewhere because there should have been about 306 items. First I checked through all the code of the app for the number "30" and found nothing. So I checked through the PHP file and discovered that someone had snuck in this:

    $rows_per_page = 30;
    

    Changed "30" to "3000" and now all is right with the world. Thank you Rich Tolley for your help.