 |
 |
|
 |
06-10-2009, 07:29 AM
|
#1 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 23
|
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.
|
|
|
06-10-2009, 12:09 PM
|
#2 (permalink)
|
|
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,449
|
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.
__________________
Last edited by smasher; 06-11-2009 at 02:09 AM.
|
|
|
06-10-2009, 08:56 PM
|
#3 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 23
|
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.
|
|
|
06-11-2009, 02:08 AM
|
#4 (permalink)
|
|
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,449
|
Quote:
Originally Posted by canit210
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];
__________________
|
|
|
06-11-2009, 05:56 AM
|
#5 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 23
|
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?
|
|
|
06-11-2009, 06:06 AM
|
#6 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 23
|
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.
|
|
|
06-11-2009, 07:47 PM
|
#7 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 23
|
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.
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 520 |
| 57 members and 463 guests |
| alexcrick209, atsd, AXP, bbc z, BostonMerlin, brianmethod, cakeisalie, cornishGeeza, davek, dbarrett, dda, deden, DevJustin, Dorald, embedded, Gamer211, ggalante, gonk, harrytheshark, iPhoneDevelopment, IphoneSdk, Jeremy1026, jharrah, kjelleinar, krowczyk, Link, markbuchanan, MarkC, masc2279, mebarron, naomipunkclan, nibby, Noise, pbcbvba, pieter78, raheel, Sckah, Sesa, SimonK, sjdev, Snappy, soulless, StefanL, Tambourin, the1nz4ne, treazer, tturk, Type25, virvalid, Vivek Nirkhe, Vonswanko, wassupdoc, winklllll, x2on |
| Most users ever online was 779, 05-11-2009 at 10:55 AM. |
» Stats |
Members: 21,507
Threads: 35,791
Posts: 156,790
Top Poster: smasher (2,449)
|
| Welcome to our newest member, soulless |
|