1

Here i used the following code to shuffle the NSMuttable Array, While shuffling The array repeats only one value and remaining all works properly, what change should i made to shuffle without repeating?

words = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3",@"4", nil] ;
NSUInteger count = [words count];

for (int i = 0; i < count; i++)
   {
    NSInteger nElements = count - i;
    NSInteger n = (arc4random() % nElements) + i;
    NSLog(@"n val %d i val %d",i,n);
    [words exchangeObjectAtIndex:i withObjectAtIndex:n];
   }

NSlog Output

2012-12-31 12:49:05.730 quizer[1607:c07] n val 0 i val 0
2012-12-31 12:49:05.731 quizer[1607:c07] n val 1 i val 3
2012-12-31 12:49:05.731 quizer[1607:c07] n val 2 i val 2
2012-12-31 12:49:05.732 quizer[1607:c07] n val 3 i val 3

Please Help me to solve

Dev_iOS
  • 251
  • 1
  • 2
  • 8
  • What you want? each value just once? – Anoop Vaidya Dec 31 '12 at 07:22
  • @AnoopVaidya Yes i want each value at once – Dev_iOS Dec 31 '12 at 07:25
  • @Dev_iOS Note that it's not the value in the array that is duplicated, it's the random number. And don't expect random numbers not to repeat, repetition is part of randomness. The decimal representation of Pi contains `3141592653` somewhere in the middle... –  Dec 31 '12 at 07:32
  • @H2CO3 Yes, you're right. So I think each time while getting random number( in for loop) he has to check for existence of value in array. As I have edited my answer.. – Krunal Dec 31 '12 at 07:35
  • @Goti Well, I'm not sure what you mean by that, but for shuffling an array, this algorithm seems good. –  Dec 31 '12 at 07:36
  • @H2CO3 is der any solution for array shuffling without repeating – Dev_iOS Dec 31 '12 at 07:38
  • Please check one of my answer : http://stackoverflow.com/questions/13640208/how-to-generate-non-repeating-random-number-objective-c – Anoop Vaidya Dec 31 '12 at 07:39
  • @Dev_iOS Why don't you simply generate two random numbers and exchange the objects at those indices? Repeat it a couple of times and the array will be shuffled. –  Dec 31 '12 at 07:41
  • @Dev_iOS : please check my answer, and comment is it working or if any changes required? – Anoop Vaidya Dec 31 '12 at 07:55

2 Answers2

1

This code may help you,

The shuffle array contains each number between 0 to TOTAL_NUMBER (your array size) only once.

From which you can shuffle your existing array.

@synthesize alreadyGeneratedNumbers;//this is a mutableArray.


int TOTAL_NUMBER=5;//size of array


-(int)generateRandomNumber{
    int low_bound = 0;
    int high_bound = TOTAL_NUMBER;
    int width = high_bound - low_bound;
    int randomNumber = low_bound + arc4random() % width;
    return randomNumber;
}


-(IBAction)randomNumbers:(UIButton *)sender
{

    NSMutableArray *startArray=[NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil];

    NSMutableArray *shuffle = [NSMutableArray new];

    BOOL contains=YES;
    while ([shuffle count]<5) {
        NSNumber *generatedNumber=[NSNumber numberWithInt:[self generateRandomNumber]];
        //NSLog(@"->%@",generatedNumber);

        if (![alreadyGeneratedNumbers containsObject:generatedNumber]) {
            [shuffle addObject:generatedNumber];
            contains=NO;
            [alreadyGeneratedNumbers addObject:generatedNumber];
        }
    }
    if ([alreadyGeneratedNumbers count] >= TOTAL_NUMBER) {
        NSLog(@"\nOne shuffel ended.....what to reshuffle");
        [alreadyGeneratedNumbers removeAllObjects];
    }

    NSMutableArray *shuffledArray=[NSMutableArray new];
    for (NSNumber *index in shuffle) {
        [shuffledArray addObject:[startArray objectAtIndex:[index integerValue]]];
    }
    NSLog(@"Shuffled array = %@",shuffledArray);
}
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
0

To remove duplicates from your NSMutableArray,

// Removing duplicate entry from array
NSArray *copy = [words copy];
NSInteger index = [copy count] - 1;
for (id object in [copy reverseObjectEnumerator])
{
    if ([words indexOfObject:object inRange:NSMakeRange(0, index)] != NSNotFound)
    {
        [words removeObjectAtIndex:index];
    }
    index--;
}
[copy release];

You can check it for each time you add the object.

Krunal
  • 1,318
  • 1
  • 13
  • 31
  • `initWithObjects:@"1", @"2", @"3",@"4", nil` doesn't seem to create any duplicates. –  Dec 31 '12 at 07:30
  • @Goti but it not shuflling, its shows the same ` array value 1 array value 2 array value 3 array value 4` – Dev_iOS Dec 31 '12 at 07:37