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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-10-2009, 07:29 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 68
Default Random number generation

I'm creating a program that I want to have randomly generate any number from 1 through 5. Can anybody help me out with the coding for this? I actually want to set it up like this:

(randomly generated number 1 through 5) +(static plus) (randomly generated number 1 through 5)

This would occur multiple times.

Thanks in Advance.
canit210 is offline   Reply With Quote
Old 06-10-2009, 12:09 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Do you mean you want to create a number by adding two random numbers?

Code:
int randomA = arc4random() % 6;
int randomB = arc4random() % 6;

int weightedRandom = randomA + randomB;
Or you want to create a string that looks like "3 + 2" , but the numbers are random?

Code:
int randomA = arc4random() % 6;
int randomB = arc4random() % 6;

NSString *mathQuestion = [NSString stringWithFormat: @"%d + %d", randomA, randomB]; 
//I forgot the asterisk in that last line. I'm adding it now.
__________________

Free Games!

Last edited by smasher; 06-11-2009 at 02:09 AM.
smasher is offline   Reply With Quote
Old 06-10-2009, 08:56 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 68
Default

thanks that's exactly what i was looking for.

Would that go in the .m file or the .h file?

Depending on which one, would I need to add anything to the other one. (I'm a beginning programmer so please be nice!)

Thank you.
canit210 is offline   Reply With Quote
Old 06-11-2009, 02:08 AM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by canit210 View Post
thanks that's exactly what i was looking for.

Would that go in the .m file or the .h file?

Depending on which one, would I need to add anything to the other one. (I'm a beginning programmer so please be nice!)

Thank you.
You need to declare variables in the .h file if they're used in more than one method, and you want them to keep their value. They're called instance variables.

Code:
//these would go between the interface brackets
int weightedRandom;
NSString *mathQuestion; //I forgot the asterisk last time
Any code that performs an action (as opposed to just declaring variables) goes in the .m file. If you've already declared the variables in the .h file, then the code changes a little. We don't need to declare the variables again, we can just use them:

Code:
int randomA = arc4random() % 6;
int randomB = arc4random() % 6;

weightedRandom = randomA + randomB;

int randomA = arc4random() % 6;
int randomB = arc4random() % 6;

mathQuestion = [NSString stringWithFormat: @"%d + %d", randomA, randomB];
__________________

Free Games!
smasher is offline   Reply With Quote
Old 06-11-2009, 05:56 AM   #5 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 68
Default

I've got three error messages on the .m class that say initializer element is not constant after the declaration of int randomA, int randomB, and mathQuestion = [NSString stringWithfromat...randomB];

Any suggestions?
canit210 is offline   Reply With Quote
Old 06-11-2009, 06:06 AM   #6 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 68
Default

I guess I should kind of explain what this will be used for.
Basically I want there to be 15 math equations each with different numbers generated. But I only want these numbers to be 1 through 5 each. Essentially it would look like this:

2+3 =
1 +4 =
3 + 3 =
2 + 4 =
1 +5 =
...

Once I have the coding, I'll probably need some help setting up some stuff and which objects to choose from the library in interface builder.

Thanks.
canit210 is offline   Reply With Quote
Old 06-11-2009, 07:47 PM   #7 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 68
Default

Could somebody please help!!?!?!?!?!

I posted in another thread and made the revisions accordingly (see below).

This is really starting to frustrate me. I implemented what they told me but nothing is actually showing up when I run it on the Simulator. Could you please tell me what exactly I need to add (and how) to Interface Builder so it will display the equation. I've tried label but when I Control-Drag from File's Owner to the label to connect the code to the label, nothing happens when I run it. I must be missing something.

Below is my .h file:

Code:
#import <UIKit/UIKit.h>

@interface AddTapViewController : UIViewController {
	NSString		*mathQuestion;
	NSString		*randomA;
	NSString		*randomB;
	IBOutlet		UILabel		*label;
}
@property (nonatomic, retain)	NSString	*mathQuestion;
@property (nonatomic, retain)	NSString	*randomA;
@property (nonatomic, retain)	NSString	*randomB;
@property (nonatomic, retain) UILabel *label;
- (void)randomEquation;

@end
Below is my .m file:


Code:
#import "AddTapViewController.h"

@implementation AddTapViewController
@synthesize mathQuestion;
@synthesize randomA;
@synthesize randomB;
@synthesize label;

- (void)randomEquation
{
    randomA = arc4random() % 6;
    randomB = arc4random() % 6;
    mathQuestion = [NSString stringWithFormat: @"%d + %d", randomA, randomB];
}


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


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

@end
I could really use some help. Thank you to everybody that has helped so far.
canit210 is offline   Reply With Quote
Reply

Bookmarks

Tags
add, generator, program, random

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: 905
21 members and 884 guests
2e1fmo, at0m87, aziz, Azuresilver, coding, Desert Diva, el.severo, fiftysixty, imran_ime4u, iVenh, jjaaxx44, nicko, pratikchandak, Promo Dispenser, ramonpadillas, RoryHarvey, Thomas, ziocleto, Zuningo
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,312
Threads: 89,035
Posts: 379,822
Top Poster: BrianSlick (7,086)
Welcome to our newest member, 2e1fmo
Powered by vBadvanced CMPS v3.1.0

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