创建一个非重复的随机数

时间:2013-10-20 05:31:20

标签: ios xcode while-loop

我想知道如何制作它,以便不会连续两次选择相同的图片。让我们说图片的数字是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
  }
}

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

3 个答案:

答案 0 :(得分: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模数来消除模数偏差。

答案 1 :(得分: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

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

答案 2 :(得分: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)传递。

让我知道是否有任何问题(我为我的一个项目做了所以可能不完全适合你的需要,所以随时修改它或者问我)