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)
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.
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];
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];
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.
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.