0

I use the following code to hide an UIButton from my rand_btns NSMutableArray.

int random = arc4random_uniform ([rand_btns count]);

if (random != level - 1) {
    [[rand_btns objectAtIndex:random] setHidden:YES];
} else {
    // call again this method
}

It works great, but ... it repeats. How can I stop repeating OR to check if an UIButton from my rand_btns array was hidden, don't need to include this objectAtIndex again.

I mean, if using my code, I can hide random button from my array excepting a specific objectAtIndex.

I would like to NOT REPEAT AGAIN THAN NUMBER from int random

rmaddy
  • 314,917
  • 42
  • 532
  • 579
iDev_91
  • 317
  • 8
  • 15

3 Answers3

2

You can refer to this Non-repeating arc4random_uniform. If you want to check if your button is already hidden or not, you can check button.hidden property.

Community
  • 1
  • 1
Hetal Vora
  • 3,341
  • 2
  • 28
  • 53
2

If you want a random-ish sequence of numbers that doesn't repeat, one way to do it is to fill an array with consecutive numbers and then shuffle the list so that the numbers are ordered randomly. That way, you know that each number occurs only once, but you can't predict the order that the numbers appear in.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • And if you run through the entire sequence, reshuffle and repeat. – pjs Aug 20 '13 at 20:55
  • Right. Numbers don't really have to even be consecutive, just monotonically increasing (so that you know they're unique). Then you shuffle to randomize the order. – Caleb Aug 20 '13 at 21:01
1

Random numbers can repeat, since they are random. You probably need to hold a list of the random numbers you have been served so far, and when you want a new number keep trying until you get a number that's not in your list. Also beware that if you're looking for a number that you haven't had so far then sooner or later you're going to run into an infinite loop as eventually there will be no more "free" numbers in the range you want

Derek Knight
  • 225
  • 2
  • 9