Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-01-2011, 06:52 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2011
Location: Sydney, Australia
Posts: 44
new2objectivec is on a distinguished road
Default Why these objects always have same value?

Hi,

This is my first post, just started learning objective-C. Got a problem while testing some basic objects. I have a "Tire" object, when initialised I assigned it a random number. But when I created 4 of them, how come they all have the same number?
Any help appreciated. Thanks!


Code:
//Tire.h
#import <Foundation/Foundation.h>

@interface Tire : NSObject {
	NSString *serialNo;
}

@property (nonatomic) NSString *serialNo;
@end

//Tire.m
#import "Tire.h"


@implementation Tire
@synthesize serialNo;

-(id) init {
	if (self=[super init]) {
		srandom(time(0));	
		int n = (random() % (100)) + 1;
		serialNo = [[NSString alloc] initWithFormat:@"ABC%d",n];
	}
	return (self);
}

-(NSString *) description {
	return (@"I am a tire, serialNo is %@",self.serialNo);
}

-(void) dealloc {
	[serialNo release];
	[super dealloc];
}

//CmdObjTest.m
#import <Foundation/Foundation.h>
#import "Tire.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // insert code here...
    NSLog(@"Hello, World!");
	
	Tire *n1 = [[Tire alloc] init];
	
	NSLog(@"Tire1: %@",n1);
	
	Tire *n2 = [[Tire alloc] init];
	
	NSLog(@"Tire2: %@",n2);
	
	Tire *n3 = [[Tire alloc] init];
	
	NSLog(@"Tire3: %@",n3);
	
    [pool drain];
    return 0;
}

@end
This is the output, I was expecting each object showing different number??

Code:
2011-06-01 20:42:50.885 CmdObjTest[1621:a0f] Hello, World!
2011-06-01 20:42:50.888 CmdObjTest[1621:a0f] Tire1: ABC25
2011-06-01 20:42:50.888 CmdObjTest[1621:a0f] Tire2: ABC25
2011-06-01 20:42:50.888 CmdObjTest[1621:a0f] Tire3: ABC25

2011-06-01 20:46:49.333 CmdObjTest[1647:a0f] Hello, World!
2011-06-01 20:46:49.335 CmdObjTest[1647:a0f] Tire1: ABC96
2011-06-01 20:46:49.336 CmdObjTest[1647:a0f] Tire2: ABC96
2011-06-01 20:46:49.336 CmdObjTest[1647:a0f] Tire3: ABC96
new2objectivec is offline   Reply With Quote
Old 06-01-2011, 11:29 AM   #2 (permalink)
Knows SQL
 
iisword's Avatar
 
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 529
iisword is on a distinguished road
Default

Try arc4random() instead of random()
__________________

Last edited by iisword; 06-01-2011 at 11:33 AM.
iisword is offline   Reply With Quote
Old 06-01-2011, 12:03 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 280
dapis is on a distinguished road
Default

You should seed the random number generator with a single call to

srandom(time(NULL));

in the applicationDidFinishLaunching method (of the app delegate) and not in the init method of your class.

Or, vastly better, do as iisword said and use arc4random. It is a far better random number generator and does not need to be seeded.
dapis is offline   Reply With Quote
Old 06-03-2011, 04:21 AM   #4 (permalink)
Registered Member
 
Join Date: Jun 2011
Location: Sydney, Australia
Posts: 44
new2objectivec is on a distinguished road
Default

As suggested, I changed the init method to as below
Code:
-(id) init {
if (self=[super init]) {
	//srandom(time(0));
	
	int n = (arc4random() % (100)) + 1;
	
	serialNo = [[NSString alloc] initWithFormat:@"ABC%d",n];
	
}
return (self);
}
And it works beautifully! Thanks a lot!

The result:

Code:
2011-06-03 19:19:40.849 CmdObjTest[237:a0f] Hello, World!
2011-06-03 19:19:40.852 CmdObjTest[237:a0f] Tire1: ABC46
2011-06-03 19:19:40.852 CmdObjTest[237:a0f] Tire2: ABC13
2011-06-03 19:19:40.853 CmdObjTest[237:a0f] Tire3: ABC68

2011-06-03 19:21:19.068 CmdObjTest[264:a0f] Hello, World!
2011-06-03 19:21:19.070 CmdObjTest[264:a0f] Tire1: ABC45
2011-06-03 19:21:19.070 CmdObjTest[264:a0f] Tire2: ABC62
2011-06-03 19:21:19.071 CmdObjTest[264:a0f] Tire3: ABC12
new2objectivec is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 488
16 members and 472 guests
7twenty7, AlanFloyd, AppsBlogger, David-T, HemiMG, iAppDeveloper, imac74, Jaxen66, lovoyl, Music Man, mutantskin, Paul Slocum, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:14 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0