How can I efficiently select several unique random numbers from 1 to 50, excluding x?

后端 未结 5 431
囚心锁ツ
囚心锁ツ 2021-01-06 19:01

I have 2 numbers which are between 0 and 49. Let\'s call them x and y. Now I want to get a couple of other numbers which are not x or y, but are al

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-06 19:33

    First, make a set of valid numbers:

    // Create a set of all the possible numbers
    NSRange range = { 0, 50 };// Assuming you meant [0, 49], not [0, 49)
    NSMutableSet *numbers = [NSMutableSet set];
    for (NSUInteger i = range.location; i < range.length; i++) {
        NSNumber *number = [NSNumber numberWithInt:i];
        [numbers addObject:number];
    }
    
    // Remove the numbers you already have
    NSNumber *x = [NSNumber numberWithInt:(arc4random() % range.length)];
    NSNumber *y = [NSNumber numberWithInt:(arc4random() % range.length)];
    NSSet *invalidNumbers = [NSSet setWithObjects:x, y, nil];
    [numbers minusSet:invalidNumbers];
    

    Then, if you don't need the numbers to be guaranteed to be random, you could use -anyObject and -removeObject to pull out a couple of other numbers. If you do need them to be random, then follow LBushkin's answer, but be careful not to accidentally implement Sattolo's algorithm:

    // Shuffle the valid numbers
    NSArray *shuffledNumbers = [numbers allObjects];
    NSUInteger n = [shuffledNumbers count];
    while (n > 1) {
        NSUInteger j = arc4random() % n;
        n--;
        [shuffledNumbers exchangeObjectAtIndex:j withObjectAtIndex:n];
    }
    

提交回复
热议问题