0

Possible Duplicate:
What’s the Best Way to Shuffle an NSMutableArray?

Here in my code I want to shuffle the elements in the array and try to print it out, but it's not shuffling properly, I tried what is shown here.Its working but,it values are repating.

for(UIView *view in self.view.subviews)
{
    if([view isKindOfClass:[UIButton class]])
    { 
        button= (UIButton *)view;
        if (button.tag >=1 && button.tag <=20)
        {
            for (NSUInteger i = 0; i < count; ++i)
            {
                [texts rotate];
                // Select a random element between i and end of array to swap with.
                int nElements = count - i;
                int n = (arc4random() % nElements) + i;
                [texts exchangeObjectAtIndex:i withObjectAtIndex:n];
                //int myTag= j+1;
                //button = [self.view viewWithTag:myTag];
                name=[NSString stringWithFormat:@"%@",[texts objectAtIndex:n]];
                [button setTitle:name forState:UIControlStateNormal];
                NSLog(@"current  name :%@",name);

            }
        }

    }

}

After shuffling the array values are repeating,pls help me to solve this issue

Community
  • 1
  • 1
Fazil
  • 1,390
  • 2
  • 13
  • 26
  • What exactly isn't working? Is it not shuffling? – 8vius Nov 03 '12 at 05:57
  • … and [iphone - nsarray/nsmutablearray - re-arrange in random order](http://stackoverflow.com/questions/4202102/iphone-nsarray-nsmutablearray-re-arrange-in-random-order). – rob mayoff Nov 03 '12 at 06:00
  • … and [how to randomize an NSMutableArray](http://stackoverflow.com/questions/6255369/how-to-randomize-an-nsmutablearray). – rob mayoff Nov 03 '12 at 06:00
  • If you don't think any of those solves your problem, please edit your post to explain exactly what you're seeing that you think is incorrect output. – rob mayoff Nov 03 '12 at 06:03
  • @8vius its not print its shwoing error – Fazil Nov 03 '12 at 06:10
  • 1
    Adding the error to the question might give people a better idea of the problem. – 8vius Nov 03 '12 at 06:16
  • Instead of if(button.tag == 1||button.tag == 2||..||button.tag == 20), why cant you use if (button.tag >=1 && button.tag <=20)? Short and simple. Easy to understand. Also what exactly is the error with above code? – iDev Nov 03 '12 at 06:55
  • @ACB i update the code in my question, here now the probem problem is repeatation of array contents – Fazil Nov 03 '12 at 07:55
  • @8vius i update the quest part pls help me – Fazil Nov 03 '12 at 07:57
  • So my understanding is that you want to shuffle texts once and then assign the objects in that shuffled array to the buttons in self.view subviews, right? And you dont want to have buttons having same name and it should be unique. is that correct? – iDev Nov 03 '12 at 08:56
  • @ACB exactly that is what i want – Fazil Nov 05 '12 at 04:55
  • Raise it as a new question. It is not exactly about shuffling the array. Your problem is that button names are repeating. I think due to the question title, they have closed it. – iDev Nov 05 '12 at 05:39
  • @ACB here i detailed http://stackoverflow.com/questions/13227350/shuffle-nsmutablearray-without-repeatation-and-showing-in-a-uibutton pls try to get me the solution yaar – Fazil Nov 05 '12 at 06:57
  • @fasilmoham Sure, I will add the answer there.. – iDev Nov 05 '12 at 07:03

2 Answers2

0

Try this int n = (arc4random() % count);
instead of
int n = (arc4random() % nElements) + i;

Ensure that you are swapping current element with any element in the texts instead of an element which is always succeeding it.

Srinivas
  • 1,780
  • 1
  • 14
  • 27
0

Just make the category of NSMutableArray

@implementation NSMutableArray (ArrayUtils)
-(void)shuffle
{
static BOOL seeded = NO;
 if(!seeded)
{
    seeded = YES;
    srandom(time(NULL));
}

NSUInteger count = [self count];
for (NSUInteger i = 0; i < count; ++i) {
    // Select a random element between i and end of array to swap with.
    int nElements = count - i;
    int n = (random() % nElements) + i;
    [self exchangeObjectAtIndex:i withObjectAtIndex:n];
}

}

SachinVsSachin
  • 6,401
  • 3
  • 33
  • 39