0

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

另外,我如何告诉 for 循环从数组中的某些值而不是从所有值中进行选择,这样我就不必为测验中的每个问题制作一个数组列表?就像我的数组中有 [abcdefg] 一样,我希望问题 1 仅以随机顺序显示 [abce]。

这是我当前的代码的样子:

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

4 回答 4

5

只需将您的问题转换为洗牌问题。

您可以使用Fisher-Yates 洗牌算法来洗牌数组

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];
于 2013-01-21T05:19:56.620 回答
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];  
}
于 2013-01-21T04:25:12.397 回答
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];
}
于 2013-01-21T03:01:43.587 回答
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]; }
于 2013-01-21T05:13:12.220 回答