I'm new to objective c and iPhone development. I'm creating an app that uses the random() function. It works but the problem I'm having is that the sequences it generates are the same every time. Here is my code:
I'm new to objective c and iPhone development. I'm creating an app that uses the random() function. It works but the problem I'm having is that the sequences it generates are the same every time. Here is my code:
What a lot of people don't realize at first is that the random number generating function random() isn't a true random number generator. In order for random() to generate a different set of numbers every time you run a program, you have to seed it with a number that's never going to be the same. To accomplish this, most people will seed it with time like this:
Code:
srand(time(NULL));
That way, it's seeded with a different number (the number of seconds that have elapsed since some time in the 1970's) and it will generate a new set of numbers every time it's launched.
As a side note, seeding the random number generator with a constant integer value makes debugging programs that use random numbers easier since the random set is the same every time. Then once your done debugging, seed random with time and you're good to go.
I wouldn't use random() or srand() if I were you - they are notoriously poor PRNGs. They're fine for very simple games, but for anything more involved, you should consider using a different. There are several available on the iPhone, including arc4random() which is easy to use and respectably random.
If you're looking for something fast, there's a good, fast PRNG in the Touch Fighter game source code that you can get if went to WWDC this year - it's on the attendee site, and should still be available.
I decided to use the arc4random() and it seems to work very well. I am only developing a very simple game/puzzle to help me learn so I don't thing I will need to look at PRNG just yet although I will check it out. Thanks for the advice.
I'm a little late on this thread but I had a question similar to this, except when I implement this code. I get 3 error messages that all say "initializer element is not constant"
How can i fix this?
Thanks in advance. New programmer here so be easy.
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientationUIIn terfaceOrientation)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
}
I'm a little late on this thread but I had a question similar to this, except when I implement this code. I get 3 error messages that all say "initializer element is not constant"
How can i fix this?
Thanks in advance. New programmer here so be easy.
Code: .m file
Code:
#import "AddTapViewController.h"
@implementation AddTapViewController
@synthesize mathQuestion;
@synthesize randomA;
@synthesize randomB;
// the following three lines don't belong here
randomA = 1 + 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
Couple things. First, don't use QUOTE tags around your code, because it doesn't get automatically included in any replies. Use the CODE tags instead.
Next, see my notes in RED in your code above. Basically, those lines are hanging out in no-man's-land in your class implementation, so the compiler thinks they're being initialized, hence the "initialization" error you're getting. This code needs to be moved into a function somewhere, either into an init function or a function that prepares to display the data to the user.
OK... This is really starting to frustrate me. I implemented what you 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.
Have you read the application tutorial in the Apple docs? It's in the guides section. This is basic Interface Builder, and the professional docs are going to do a better job explaining the basics than anyone can in a quick post here.
randomEquation is never being called. You either need to change the name to viewDidLoad, or add viewDidLoad and call randomEquation from it. Also, the label's text is never being set, you have to add label.text = mathQuestion; You also need to make randomA and randomB ints not strings.
Have you read the application tutorial in the Apple docs? It's in the guides section. This is basic Interface Builder, and the professional docs are going to do a better job explaining the basics than anyone can in a quick post here.
joe
Meh... but who has the time or desire to actually learn something? We all want to get stuff done! Reading documentation that explains how things work is soooo overrated.