★ TouchArcade needs your help. Click here to support us on Patreon.

Help with random numbers

02-08-2009, 10:36 AM
#1
Joined: Jan 2009
Posts: 87
Help with random numbers

Hello I am trying to make a random number generator that will display the random number in a text box but xcode is not cooperating with me so I you could help me that would be great, here is my code.

Code:
#import "UIKit/UIKit.h"
#import "AttackViewController.h"

@implementation AttackViewController
@synthesize statusText;

-(IBAction)buttonPressed:(id)sender;
{
	
	NSString *attack = arc4random() %20 + 1;
	NSString *newText = [[NSString alloc] initWithFormat:
		@"Your attack is %@.", attack];
	statusText.text = newText;
	[newText release];
}

- (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 {
	[statusText release];
    [super dealloc];
}

@end
This is from the AttackViewController.m file

I keep getting this warning.
warning: initialization makes pointer from integer without a cast


Thanks in advance.
02-08-2009, 11:40 AM
#2
Quote:
Originally Posted by Drenguin View Post
Code:
	
	NSString *attack = arc4random() %20 + 1;
I keep getting this warning.
warning: initialization makes pointer from integer without a cast
That particular warning is because of that line of code. You are generating a random number with arc4random, but then you are assigning that number directly to a string. Try instead something like,

Code:
int myRandomNum = arc4random() %20 +1;
NSString* attack = [NSString stringWithFormat:@"%i", myRandomNum];
We have over an hour of free tutorials on our website for budding developers. Check them out!

02-08-2009, 12:38 PM
#3
Joined: Jan 2009
Posts: 87
Thank you so much I will try this out.

Also, I have been on your website for the past couple weeks and it is so helpful so thank you for that too!
02-11-2009, 10:13 PM
#4
Joined: Jan 2009
Posts: 87
How to convert strings to numbers

I am trying to do math with strings but I realize that I have to make the string a number, how would I do that?

Here is one of the strings I am trying to convert.

Code:
int myRandomNum = arc4random() %20 +1;
	NSString *attack = [NSString stringWithFormat:@"%i", myRandomNum];
02-11-2009, 10:23 PM
#5
To convert from a string to a int, double, or float you'd do the following

Code:
NSString* yourString = @"50"; //Whatever it happens to be

int myInt = [yourString intValue];
double myDouble = [yourString doubleValue];
float myFloat = [yourString floatValue];
02-14-2009, 06:49 PM
#6
Joined: Jan 2009
Posts: 87
Thank you so much for what you have helped me with so far but I have another question. I am trying to return health but it is already set to 100, how do I reset it to a different number.

Here is my code

Code:
@implementation AttackViewController
@synthesize statusText;



-(IBAction)buttonPressed:(id)sender;
{
	int health = 100;
	int myRandomNum = arc4random() %20 +1;
	NSString *attack = [NSString stringWithFormat:@"%i", myRandomNum];
	int attackDamage =  [attack intValue];
	int damageDealt = health - attackDamage;
	NSString *damage = [NSString stringWithFormat:@"%i", damageDealt];

	NSString *newText = [[NSString alloc] initWithFormat:
		@"%@.", damage];
	
	statusText.text = newText;
	[newText release];
	return health;
}
02-15-2009, 04:23 PM
#7
Quote:
Originally Posted by Drenguin View Post
Thank you so much for what you have helped me with so far but I have another question. I am trying to return health but it is already set to 100, how do I reset it to a different number.

Here is my code

Code:
@implementation AttackViewController
@synthesize statusText;



-(IBAction)buttonPressed:(id)sender;
{
	int health = 100;
	int myRandomNum = arc4random() %20 +1;
	NSString *attack = [NSString stringWithFormat:@"%i", myRandomNum];
	int attackDamage =  [attack intValue];
	int damageDealt = health - attackDamage;
	NSString *damage = [NSString stringWithFormat:@"%i", damageDealt];

	NSString *newText = [[NSString alloc] initWithFormat:
		@"%@.", damage];
	
	statusText.text = newText;
	[newText release];
	return health;
}
Without knowing what you're wanting to do with health it is somewhat difficult to give you the best most succinct answer. Judging from what I can see of your code, it looks like you have a UILabel statusText that shows the changes each time a button is pressed to represent the latest amount of damage dealt. I'm assuming that since you don't have health set up with a @property & @synthesize that you intend to keep that information in the background, a.k.a. you won't be presenting it to the user.

Again, assuming, that you wish to keep track of health over the course of the game, you'll need to declare it outside of that method. Otherwise each time you run through that method it'll get reset back to 100. Thankfully, this is incredibly easy to do. Simply take

Code:
int health = 100;
and stick it right below

Code:
@synthesize statusText;
We've just changed health from being a local variable (only visible within that one method, to being an instance variable (visible within the entire class, so now other methods inside AttackViewController.m can use it).

Now each time we run through that method, health doesn't get reset back to 100. To get to your question,
Quote:
how do I reset it to a different number
simply take an int value of some kind (presumably one that you've calculated based off damage) and assign it to health.

aka

Code:
int damageDealt = 50; //Or whatever # you desire, including randoms
health -= damageDealt;
"-=" is legit coding shorthand for, health = health - damageDealt; Also note that we didn't redeclare health;

Code:
int health -= damageDealt;
That is a no no!

One final note:

Code:
int myRandomNum = arc4random() %20 +1;
NSString *attack = [NSString stringWithFormat:@"%i", myRandomNum];
int attackDamage =  [attack intValue];
Can be abbreviated to:

Code:
int attackDamage = arc4random()%20+1;
03-01-2009, 04:05 PM
#8
Joined: Jan 2009
Posts: 87
Sorry that I didn't specify what I wanted, I want health to begin at 100 and decrease by a random number every time you click the button. However it keeps resetting health back to 100, am I returning it wrong? I keep getting the error "'return' with a value, in function returning void."

Here is my code again, Thank you in advance.
Code:
#import "UIKit/UIKit.h"
#import "AttackViewController.h"

@implementation AttackViewController
@synthesize statusText;
int health = 100;


-(IBAction)buttonPressed:(id)sender;
{

	int attackDamage = arc4random() %20 +1;

	int damageDealt = health - attackDamage;
	NSString *damage = [NSString stringWithFormat:@"%i", damageDealt];

	NSString *newText = [[NSString alloc] initWithFormat:
		@"%@.", damage];
	statusText.text = newText;
	[newText release];
	return health;
}
03-01-2009, 04:32 PM
#9
Quote:
Originally Posted by Drenguin View Post
Sorry that I didn't specify what I wanted, I want health to begin at 100 and decrease by a random number every time you click the button. However it keeps resetting health back to 100, am I returning it wrong? I keep getting the error "'return' with a value, in function returning void."

Here is my code again, Thank you in advance.
Code:
#import "UIKit/UIKit.h"
#import "AttackViewController.h"

@implementation AttackViewController
@synthesize statusText;
int health = 100;


-(IBAction)buttonPressed:(id)sender;
{

	int attackDamage = arc4random() %20 +1;

	int damageDealt = health - attackDamage;
	NSString *damage = [NSString stringWithFormat:@"%i",damageDealt];

	NSString *newText = [[NSString alloc] initWithFormat:
		@"%@.", damage];
	statusText.text = newText;
	[newText release];
	return health;
}
Okay, so what we need to do is something like,


Code:
#import "UIKit/UIKit.h"
#import "AttackViewController.h"

@implementation AttackViewController
@synthesize statusText;
int health = 100;


-(IBAction)buttonPressed:(id)sender;
{
	int attackDamage = arc4random() %20 +1;

	health -= attackDamage;
	NSString *damage = [NSString stringWithFormat:@"%i",      attackDamage]; 
//This puts the random number into a string

	NSString *newText = [[NSString alloc] initWithFormat:
		@"%@.", damage];
	statusText.text = newText;
	[newText release];

NSString *newHealth = [[NSString alloc] initWithFormat:
		@"%i.", health];
	//If You want to do something with the health as a string, do it here
	[newHealth release];
}
03-01-2009, 06:12 PM
#10
Joined: Jan 2009
Posts: 87
Thank you so much you are amazing, but I have another problem. I got it to work and now I am trying to tell it to display "You Win" if health is less than or equal to 0 but I keep getting the error "syntax error before health" again here is my code. Thank You.

Code:
@implementation AttackViewController
@synthesize statusText;
int health = 100;


-(IBAction)buttonPressed:(id)sender;
{
	int attackDamage = arc4random() %20 +1;
	
	health -= attackDamage;
	if health <= 0;
	{
		NSString *youWin = [[NSString alloc] initWithFormat:
							 "You Win"];
		statusText.text = youWin;
	}
	NSString *damage = [NSString stringWithFormat:@"%i",      health]; 
	//This puts the random number into a string
	
	NSString *newText = [[NSString alloc] initWithFormat:
						 @"%@.", damage];
	statusText.text = newText;
	[newText release];
	NSString *newHealth = [[NSString alloc] initWithFormat:
						   @"%i.", health];
	//If You want to do something with the health as a string, do it here
	[newHealth release];
}