0

我想知道如何制作它,以免连续两次选择同一张图片。可以说图片的编号为 1-3。如果选择了图片 1,那么接下来不会选择图片 1。如果选择了图片 3,则可以再次选择图片 1,以此类推。

我知道我必须使用一个while语句,但我不知道如何使用。这是我现在所拥有的:

- (void)chooseBackgroundImage{
  if(thisNumber % 10 == 0){
    int chooseBackgroundImage = arc4random() % 7;
    switch (chooseBackgroundImage) {
        case 0:
            backgroundImage.image = [UIImage imageNamed:@"CyanToYellowBackground.png"];
            break;
        case 1:
            backgroundImage.image = [UIImage imageNamed:@"GreenToBlueBackground.png"];
            break;
        case 2:
            backgroundImage.image = [UIImage imageNamed:@"OrangeToGreenBackground.png"];
            break;
        case 3:
            backgroundImage.image = [UIImage imageNamed:@"OrangeToPurpleBackground.png"];
            break;
        case 4:
            backgroundImage.image = [UIImage imageNamed:@"PurpleToCyanBackground.png"];
            break;
        case 5:
            backgroundImage.image = [UIImage imageNamed:@"RedToBlueBackground.png"];
            break;
        case 6:
            backgroundImage.image = [UIImage imageNamed:@"YellowToRedBackground.png"];
            break;
       }
    }
}

我也尝试过使用:

- (void)chooseBackgroundImage{
if(slogansGenerated % 10 == 0){
    int chooseBackgroundImage = arc4random() % 7;
    while(chooseBackgroundImage == oldChooseBackgroundImage){
    switch (chooseBackgroundImage) {
        case 0:
            backgroundImage.image = [UIImage imageNamed:@"CyanToYellowBackground.png"];
            break;
        case 1:
            backgroundImage.image = [UIImage imageNamed:@"GreenToBlueBackground.png"];
            break;
        case 2:
            backgroundImage.image = [UIImage imageNamed:@"OrangeToGreenBackground.png"];
            break;
        case 3:
            backgroundImage.image = [UIImage imageNamed:@"OrangeToPurpleBackground.png"];
            break;
        case 4:
            backgroundImage.image = [UIImage imageNamed:@"PurpleToCyanBackground.png"];
            break;
        case 5:
            backgroundImage.image = [UIImage imageNamed:@"RedToBlueBackground.png"];
            break;
        case 6:
            backgroundImage.image = [UIImage imageNamed:@"YellowToRedBackground.png"];
            break;
    }
    int oldChooseBackgroundImage = chooseBackroundImage
  }
}

但似乎没有任何效果。有没有办法创建一个不重复的随机数?

4

3 回答 3

2

我猜你正在寻找这样的东西。

-(int)getNonRepeatedRandom{
    int randomNumber = -1;
    do{
        randomNumber = arc4random_uniform(7);
    } while (randomNumber == oldRandomNumber);
    oldRandomNumber = randomNumber; //set the random number to old random so you can check it on the next run.
    return randomNumber;
}

oldRandomNumber 必须是 iVar 才能正常工作。

您应该使用 arc4random_uniform 而不是 arc4random 模数来消除模数偏差。

于 2013-10-20T05:45:58.790 回答
2

以下内容可能足以满足您的需要:

首先添加一个实例变量,例如lastChosenBackgroundImage.

后:

int chooseBackgroundImage = arc4random() % 7;

添加:

if(chooseBackgroundImage == lastChosenBackgroundImage)
   chooseBackgroundImage = (chooseBackgroundImage + 1) % 7; // same as last time, move to next choice
lastChosenBackgroundImage = chooseBackgroundImage; // remember for next time

这确实意味着选择下一个图像的可能性是选择其他图像的两倍,但我怀疑这对您的用例来说不是一个重大问题。

于 2013-10-20T07:51:24.127 回答
0

使用此实用程序函数为您提供范围之间的随机整数(在您的情况下为 0 和 6) -

#define MAX_ATTEMPTCOUNT 10
// Provides a random number between the range (both inclusive).
+ (int)randomIntegerInRange:(int)fromInt toInteger:(int)toInt excluding:(NSArray *)excludeNumbers {
    NSAssert((toInt - fromInt) > 0 && (!excludeNumbers ? YES : (toInt - (fromInt - 1)) >= [excludeNumbers count]), @"Invalid range");
    static int randomAttempts = 0;

    srandom(time(NULL));
    int randomInteger = fromInt + random() % (toInt - (fromInt - 1));

    if (excludeNumbers != nil) {
        for (NSNumber *number in excludeNumbers) {
            if ([number intValue] == randomInteger) {
                if (randomAttempts == MAX_ATTEMPTCOUNT) {
                    // Reached the maximum attempt count to get the random number but failed to find one.
                    break;
                }
                else {
                    // Recursive call to get obtain the next number.
                    ++randomAttempts;
                    randomInteger = [self randomIntegerInRange:fromInt toInteger:toInt excluding:excludeNumbers];
                }
                break;
            }
        }

        if (randomAttempts >= MAX_ATTEMPTCOUNT) {
            // Pick up the first number that's not there in visited words.
            randomAttempts = 0; // Reset the counter for next attempt.
            randomInteger = fromInt;

            for (; randomInteger <= toInt; ++randomInteger) {
                bool found = NO;
                for (NSNumber *number in excludeNumbers) {
                    if ([number intValue] == randomInteger) {
                        // Found the number.
                        found = YES;
                        break;
                    }
                }
                if (!found) break;
                else continue;
            }
        }
    }

    return randomInteger;
}

将返回的整数添加到实际上作为参数 (excludeNumbers) 传递的数组(排除数字的数组)中。

如果有任何问题,请告诉我(我是为我的一个项目制作的,因此可能不完全适合您的需要,因此请随时修改或询问我)

于 2013-10-20T05:40:20.997 回答