Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 3, 2009, 09:54 AM   #1
GAx
macrumors newbie
 
Join Date: Sep 2008
check if random number is already in array

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)myActionid)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);
}
GAx is offline   0 Reply With Quote
Old Apr 4, 2009, 04:56 AM   #2
madivad
macrumors newbie
 
Join Date: Apr 2009
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:
Code:
-(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)
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
madivad is offline   0 Reply With Quote
Old Apr 7, 2009, 05:50 AM   #3
GAx
Thread Starter
macrumors newbie
 
Join Date: Sep 2008
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.co...ng-arrays.html

This will printout myArray as a non repetitive 50 random number array
-------------------------------------------
-(IBAction)myActionid)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 addObjectbjectToMove];
[copy removeObjectAtIndex:index];
}

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

}

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

best,
G
GAx is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
RAID array failing, random drive ejects. How to check for errors and replace drive? zopiro Mac Pro 2 Nov 15, 2013 10:40 PM
Good, Portable Random Number Generator for C++? ArtOfWarfare Mac Programming 19 Jan 10, 2013 05:59 PM
Interesting refurb serial number check Trj MacBook Pro 2 Nov 28, 2012 01:19 AM
How can I take a random object from one array, and insert to another? **Help** iJustinCabral iPhone/iPad Programming 18 Aug 22, 2012 11:25 AM
Creating a random number within a URL using Applescript moorbull Mac Programming 1 Jul 19, 2012 08:16 AM

Forum Jump

All times are GMT -5. The time now is 04:32 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC