如何从数组生成随机值而不重复Objective-C中的任何值

时间:2013-01-21 02:54:04

标签: objective-c xcode

现在我已经设法在我的按钮上显示随机文本,我注意到大多数时候数组中的项重复自己。所以我得到类似“a b b b”或“a c b c”而不是“a b c d”或“a c d b”。如何告诉我的for循环排除已经使用过的数组值的索引,这样它就不会重复了?

另外,如何告诉for循环从数组中的某些值中选择而不是从所有内容中选择,这样我就不必为测验中的每个问题创建一个数组列表?就像我的数组中有[a b c d e f g]一样,我希望问题1只能以随机顺序显示[a b c e]。

以下是我当前代码的样子:

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]; }

4 个答案:

答案 0 :(得分:5)

简单地将您的问题转化为混乱问题。

您可以使用Fisher–Yates shuffle algorithm来重新排列数组

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];
}

然后从中获取前n个对象:您将从原始数组中获得n个随机唯一元素。

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

答案 1 :(得分:3)

试试这个,

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];  
}

答案 2 :(得分:1)

while用于测试随机结果是否有效,这是一个坏主意。理论上,一串随机数可能导致CPU锁定很长时间,尽管不太可能。如果每个答案只能循环一次循环,那就好多了。

而是尝试制作数组的副本,然后在选择下一个项目之前删除您使用过的任何项目。

// 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];
}

答案 3 :(得分:0)

    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]; }