user1597438
user1597438

Reputation: 2221

How to generate random values from an array without repeating any in Objective-C

Now that I have managed to display random texts on my button, I noticed how most of the times the items inside the array repeat itself. So I get something like "a b b b" or "a c b c" instead of "a b c d" or "a c d b". How do I tell my for loop to exclude the index of the array value that's already been used so it won't repeat any at all?

Also, how do I tell the for loop to choose from certain values in the array instead of from everything so I won't have to make an array list for each question on the quiz? Like if I have [a b c d e f g] in my array, I want question 1 to display only [a b c e] in random order.

Here's how my current code looks like:

answerList = [[NSMutableArray alloc] initWithObjects:
"a", "b", "c", "d", "e", "f", "g", nil];
    for (int j=0; j<answerList.count; j++)
{
    int k = arc4random() % [answerList count];
    [btnA setTitle:[answerList objectAtIndex:k] forState:UIControlStateNormal];
    [answerList removeObjectAtIndex:k];
    int l = arc4random() % [answerList count];
    [btnB setTitle:[answerList objectAtIndex:l] forState:UIControlStateNormal];
    [answerList removeObjectAtIndex:l];
    int m = arc4random() % [answerList count];
    [btnC setTitle:[answerList objectAtIndex:m]  forState:UIControlStateNormal];
    [answerList removeObjectAtIndex:m];
    int n = arc4random() % [answerList count];
    [btnD setTitle:[answerList objectAtIndex:n] forState:UIControlStateNormal];
    [answerList removeObjectAtIndex:n]; }

Upvotes: 0

Views: 3841

Answers (4)

Gabriele Petronella
Gabriele Petronella

Reputation: 108101

Simply transform your problem into a shuffling problem.

You can use the Fisher–Yates shuffle algorithm to shuffle the array

answerList = [[NSMutableArray alloc] initWithObjects:@"a", @"b", @"c", @"d", @"e", @"f", @"g", nil];
for (int i = answerList.count - 1; i >= 0; --i) {
    int r = arc4random_uniform(answerList.count);
    [answerList exchangeObjectAtIndex:i withObjectAtIndex:r];
}

then take the first n objects from it: you will get n random unique elements from the original array.

[btnA setTitle:answerList[0] forState:UIControlStateNormal];
[btnB setTitle:answerList[1] forState:UIControlStateNormal];
[btnC setTitle:answerList[2] forState:UIControlStateNormal];
[btnD setTitle:answerList[3] forState:UIControlStateNormal];

Upvotes: 5

Madhu
Madhu

Reputation: 1542

    numberArray = [[NSMutableArray alloc]initWithCapacity:0];
    for(int i=1;i<=20;i++)
    {
        [numberArray addObject:[NSNumber numberWithInt:i]];
    }





   for (int i = 0;i<[numberArray count];i++,i--,k++)  {
        srand(time(NULL));
        a = rand()%[numberArray count];
        //NSLog(@"a Value :%@",[numberArray objectAtIndex:a]);
        tempStr1 = [numberArray objectAtIndex:a];
        [numberArray removeObjectAtIndex:a];
        b=rand()%[numberArray count];
        //NSLog(@"b VAlue :%@",[numberArray objectAtIndex:b]);
        tempStr2 = [numberArray objectAtIndex:b];
        [numberArray removeObjectAtIndex:b];

        c = rand()%[numberArray count];
        //NSLog(@"a Value :%@",[numberArray objectAtIndex:a]);
        tempStr3 = [numberArray objectAtIndex:c];
        [numberArray removeObjectAtIndex:c];
        d=rand()%[numberArray count];
        //NSLog(@"b VAlue :%@",[numberArray objectAtIndex:b]);
        tempStr4 = [numberArray objectAtIndex:d];
        [numberArray removeObjectAtIndex:d]; }

Upvotes: 0

Anusha Kottiyal
Anusha Kottiyal

Reputation: 3905

Try this,

int random;

NSString *currentText;

nonrepeatingArray = [[NSMutableArray alloc] init];

buttonArray = [[NSMutableArray alloc] initWithObjects:btnA, btnB, btnC, btnD, btnE, btnF, btnG, nil];

answerList = [[NSMutableArray alloc] initWithObjects:"a", "b", "c", "d", "e", "f", "g", nil];

for (int j=0; j<answerList.count; j++)
{
    do {
          random = arc4random()%answerList.count;
          currentText = [answerList objectAtIndex:random];
       } while ([nonrepeatingArray containsObject:currentText]);

    [nonrepeatingArray addObject:currentText];

    UIButton *button = (UIButton *)[buttonArray objectAtIndex:j];

    NSString *title = [answerList objectAtIndex:random];

    [button setTitle:title forState:UIControlStateNormal];  
}

Upvotes: 3

Alex Wayne
Alex Wayne

Reputation: 187024

while used to test if a random result is valid like this is a bad idea. In theory, a string of random numbers could result in a CPU lock for a long time, though unlikely. It's much better if you can iterate the loop just once per answer.

Instead try to make a copy of the array and simply remove any item you've used before picking the next item.

// master answer list
answerList = [[NSMutableArray alloc] initWithObjects:@"a", @"b", @"c", @"d", @"e", @"f", @"g", nil];

// make a copy we can delete from
NSMutableArray *unusedAnswerList = [answerList mutableCopy];

// loop until they are all gone
while([unusedAnswerList count] > 0)
    // get a random index
    int index = arc4random()%[unusedAnswerList count];

    // pull out the value at the random index
    NSString *title = [unusedAnswerList objectAtIndex:index];

    // remove the value we just grabbed.
    [unusedAnswerList removeObjectAtIndex:index];

    // do somehting with the value we jsut grabbed
    [btnA setTitle:title forState:UIControlStateNormal];
    [btnB setTitle:title forState:UIControlStateNormal];
    [btnC setTitle:title forState:UIControlStateNormal];
    [btnD setTitle:title forState:UIControlStateNormal];
}

Upvotes: 1

Related Questions