title: iOS: How do I generate 8 unique random integers?
body: <p>I need to generate 8 random integers, but they need to be unique, aka not repeated.</p>
  
  <p>For example, I want 8 numbers within the range 1 to 8.</p>
  
  <p>I've seen arc4random but I'm not sure how to make them unique ?</p>
  
  <p><strong>Solution</strong></p>
  
  <pre><code>-(NSMutableArray *)getRandomInts:(int)amount from:(int)fromInt to:(int)toInt {
  
    if ((toInt - fromInt) +1 &lt; amount) {
        return nil;    
    }
  
    NSMutableArray *uniqueNumbers = [[[NSMutableArray alloc] init] autorelease];
    int r;
    while ([uniqueNumbers count] &lt; amount) {
  
        r = (arc4random() % toInt) + fromInt;
        if (![uniqueNumbers containsObject:[NSNumber numberWithInt:r]]) {
            [uniqueNumbers addObject:[NSNumber numberWithInt:r]];
        }
    }
    return uniqueNumbers;
  }
  </code></pre>