0

I am using arc 4 random to choose a random entry from my fetchresultscontroller but I need to leave out all entries of a certain category. The column is "Category" and I dont want the random choice to include the category "Category2". Below is my current code, any help would be greatly appreciated!

- (NSIndexPath *)randomIndexPath
{

    NSInteger randomSection = arc4random_uniform([[fetchedResultsController sections]     count]);
    id <NSFetchedResultsSectionInfo> sectionInfo =
    [[fetchedResultsController sections] objectAtIndex:randomSection];
    NSInteger randomIndex = arc4random_uniform( [sectionInfo numberOfObjects]);
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:randomIndex     inSection:randomSection];
    return indexPath;
}


- (IBAction)displayRandomEntry:(id)sender {
    if([[fetchedResultsController sections] count] <= 0)
        return;

    NSIndexPath *indexPath = [self randomIndexPath];
    [self.myTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
    id object = [fetchedResultsController objectAtIndexPath:indexPath];

    RandomEntryDetailVC * controller;

    controller = [self.storyboard     instantiateViewControllerWithIdentifier:@"ShowRandomEntryDetailVC"];

    controller.currentItem = object;
    [self.navigationController pushViewController:controller animated:YES];
    }
4

1 回答 1

1

在任何随机数生成器中,避免特定值(或值范围)的方法是选择一个数字,如果该数字不在您喜欢的范围内,则选择另一个数字。继续,直到你得到一个你喜欢的号码。虽然理论上这是非停止的,但实际上对于任何足够大的子集,它都会很快停止。

于 2013-08-24T23:55:58.793 回答