PDA

View Full Version : check if random number is already in array




GAx
Apr 3, 2009, 09:54 AM
Hi

I'm trying to compare a random number to an Array.
If the number is not in the Array, NSLog will tell me and write it to that array.
If the number is in the Array, I want it to generate a new random number and check that.

I will add code later so when the Array count hits 100, it will empty the array, that way it won't fill up.

Or any other suggestions on how to solve this would be good.

thank you

here's the method:


-(IBAction)myAction:(id)sender{

srandom(time(NULL));
int i = random() % 50 +1;
NSString *myString = [[NSString alloc] initWithFormat:@"%i", i];

if ([arr1 containsObject:myString] != YES) {

NSLog(@"Array count = %i", [arr1 count]);
NSLog(@"No, arr1 did not contain '%@', adding it now.", myString);
[arr1 addObject:myString ];
}
else {

//The random number i is already in the array, so I
//want it to generate new i and run the "if" again.
//Running another if here would not help, if the new value
//of i is in the array it would mean same problem all over again.

}
[myString release];
[arr1 retain];
NSLog(@"%@", arr1);
}



madivad
Apr 4, 2009, 04:56 AM
There's a number of ways of doing this, and it depends for how long you wish to do it for. I am not yet up with Obj-C, so I'll pseudo code it and you can take the best approach for you and implement it with a little research:

The first option just uses your code modified marginally:
-(IBAction)myAction:(id)sender{

// start a loop here and we'll test at the end
// if the random number was good or not
do { // look this up, it's like a while with the condition at the end
srandom(time(NULL));
int i = random() % 50 +1;
NSString *myString = [[NSString alloc] initWithFormat:@"%i", i];


} while ([arr1 containsObject:myString] == YES)

NSLog(@"Array count = %i", [arr1 count]);
NSLog(@"No, arr1 did not contain '%@', adding it now.", myString);
[arr1 addObject:myString ];

// You have now added one new item to your array

[myString release];
[arr1 retain];
NSLog(@"%@", arr1);
}

The way you have above (or I have chopped above) is slow and is only doing it one addition at a time. The peoblem with what you suggest is renewing it at a length of 100, but there are only 50 objects, you will never fill it.

A better approach is to:

1. create an array with the maximum number of objects, 1, 2, 3, 4... n
2. shuffle the array
3. call your array from top to bottom
(pseudo code)

// build your array
array myArray;
for (int i=50; i; i--) myArray.add i;

// shuffle array
for( var rnd, tmp, i=myArray.length; i;
rnd=parseInt(Math.random()*i),
tmp=myArray[--i],
myArray[i]=myArray[rnd],
myArray[rnd]=tmp ) ; // ';' is needed

// and you now have a shuffle array of 50 numbers
Please note, this is a bastardised javascript piece of code, but should work equally well given the right syntax in obj-C (maybe)

Good luck

GAx
Apr 7, 2009, 05:50 AM
Thanks for your advice, it really helped.
I'm pretty new to obj-C, used to do ASP and SQL back-end web solutions, but this it completely different.

I used your shuffled array approach and tweaked some obj-c code I found here:
http://iphonedevelopment.blogspot.com/2008/10/shuffling-arrays.html

This will printout myArray as a non repetitive 50 random number array
-------------------------------------------
-(IBAction)myAction:(id)sender {

NSMutableArray *myArray = [[NSMutableArray alloc] init];
int myInt = 1;

for (int i = 0; i < 50; i++) {
NSString *myString = [[NSString alloc] initWithFormat:@"%i", myInt];
[myArray addObject:myString];
myInt++;
}

NSMutableArray *copy = [myArray mutableCopy];
[myArray removeAllObjects];

while ([copy count] > 0)
{
int index = arc4random() % [copy count];
id objectToMove = [copy objectAtIndex:index];
[myArray addObject:objectToMove];
[copy removeObjectAtIndex:index];
}

[copy release];
NSLog(@"myArray values: %@", myArray);

}

-------------------------------------------
again, thanks

best,
G