0

I have code that, when I press a button, causes the label to change. I'd tried to write code so that the label doesn't get the same value twice in a row, but it didn't work because I still get the same value in the twice in a row sometimes.

What is the right solution?

This is the code:

- (IBAction)buttonPressed:(id)sender {
    if (sender == self.button) {
        // Change the randomLabel by right answer
        NSString *path = [[NSBundle mainBundle] pathForResource:@"words" ofType:@"plist"];
        words = [[NSMutableArray alloc] initWithContentsOfFile:path];
        NSString *generateRandomLabel = [NSString stringWithFormat:@"%@", [words objectAtIndex:arc4random_uniform([words count] - 1)]];

        if ([randomLabel.text isEqualToString:generateRandomLabel]) {
            while ([randomLabel.text isEqualToString:generateRandomLabel]) {
                generateRandomLabel = [NSString stringWithFormat:@"%@", [words objectAtIndex:arc4random_uniform([words count] - 1)]];
            }
        } else if (![randomLabel.text isEqualToString:generateRandomLabel]) {
            [self.randomLabel setText:generateRandomLabel];
            [randomLabel.text isEqualToString:generateRandomLabel];
        }
    }
Extra Savoir-Faire
  • 6,026
  • 4
  • 29
  • 47
T1992
  • 73
  • 8
  • 1
    Please review your formatting before posting. There is a live preview just below your post when editing. – DrummerB Dec 06 '12 at 14:05
  • Apropos of nothing, you already have an array of strings in `words`. What makes you think you have to bother with using `+[NSString stringWithFormat:]`? – Extra Savoir-Faire Dec 06 '12 at 14:33

2 Answers2

3

the problem is that the random function is generating a random value but nothing keeps it from generating the same value n times. array or set doesnt matter

you need a non-repeating random algo:

1) you should KEEP the array loaded once, not load it anew everytime

2) and then SHUFFLE the array once. (see What's the Best Way to Shuffle an NSMutableArray?)

3) THEN on a press of a button, use the 0 object in the array, remove it and readd it at the end

mock-code: Assumes words file > 1, with at least 2 different words

- init {
    self = [super init];
    words = [self loadWordsFromFile];
    [words shuffle];
}

- onButtonPress {
    id word = nil;
    do { 
        [words objectAtIndex:0];
        [words removeObjectAtIndex:0];
        [words addObject:word];
    while([word isEqualToString:[words objectAtIndex:0])
    label.text = word;
}
Community
  • 1
  • 1
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
1

You can do this very simply by having your random number generator pick from all the members of the array except the last one, and then exchange the one you pick with the last item in the array:

- (IBAction)buttonPressed:(id)sender {
    if (! words) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"words" ofType:@"plist"];
        words = [[NSMutableArray alloc] initWithContentsOfFile:path];
    }
    NSInteger index = arc4random_uniform(words.count - 2);
    randomLabel.text = words[index];
    [words exchangeObjectAtIndex:index withObjectAtIndex:words.count-1];
}
rdelmar
  • 103,982
  • 12
  • 207
  • 218