I would appreciate a tutorial on how to display a text string, or image, chosen at random from a pre-defined list. For example, I want a user to be able to push a button and have the phone randomly display one of the following responses "yes", "no", or "maybe". Thanks for any assistance.
In general, you want to make an array of your (images / strings,) then pick a random number, then grab that Item from the array. Something like this:
Code:
//create the array
NSArray *myArray= [NSArray arrayWithObjects: @"Yes",@"No",@"File Not Found", nil];
//get the length of the array
int length = [myArray count];
//choose an item - C magic happens here :)
int chosen = (float)random() * length /RAND_MAX;
//get that item
NSString *item = [myArray objectAtIndex: chosen];
//print that item
NSLog(@"The item I picked is: %@", item);
That's 4 bits of cocoa magic, and one bit of plain-old C magic.
In general, you want to make an array of your (images / strings,) then pick a random number, then grab that Item from the array.
Hi Smasher. Thanks very much for taking time to respond. I am completely new to programming, so not entirely sure how to integrate this into my app. As a placeholder, I had a button writing a text string to a label field (shown below).
Code:
- (IBAction)hello:(id)sender {
helloLabel.text = @"May the Force Be with You";
}
I tried plunking the code you shared into an IBAction in my Controller.m file, substituting the NSLog line for my helloLabel.text line, but couldn't get it to work. I'm sure this is a simple thing to implement, but it is escaping me.
I kept messing with this. Kinda got it to work, but sometimes get the same response several times in a row (i.e. not seeming very random) and am getting "Warning: local declaration of 'myArray' hides instance variable" messages.
- (IBAction)hello:(id)sender {
//create the array
NSArray *myArray= [NSArray arrayWithObjects: @"Yes",@"No",@"Maybe", nil];
//get the length of the array
int length = [myArray count];
//choose an item - C magic happens here :)
int chosen = (float)random() * length /RAND_MAX;
//get that item
NSString *item = [myArray objectAtIndex: chosen];
//print that item
helloLabel.text = (@"THe item I picked is: %@", item);
}
hello is a label field that I'm outputting the text to display on screen. Still expect I'm doing something wrong. Help greatly appreciated. Thanks.
It's always the same result, or sometimes you get the same result twice in a row? Getting duplicates in a row is not surprising - if you flip a coin 30 times, you'll see a lot of HeadHeadHead and TailTailTail.
If you want to get rid of dupes, you'll have to keep track of what the last item chosen was, and pick a new item if you pull a dupe. We could do this with a "do-while" loop.
Code:
- (IBAction)hello:(id)sender {
//"static" means this variable will keeps it value between function calls.
static int lastChosen=0;
//this is the same
NSArray *myArray= [NSArray arrayWithObjects: @"Yes",@"No",@"Maybe", nil];
int length = [myArray count];
int chosen;
//this is a "Do" loop - it repeats as long as the "while" is true.
//we'll keep picking numbers until we get not-a-dupe
do {
chosen = (float)random() * length /RAND_MAX;
} while(chosen==lastChosen);
//this is the same
NSString *item = [myArray objectAtIndex: chosen];
//this is how we combine the two strings
helloLabel.text = [NSString stringWithFormat: @"The item I picked is: %@", item];
//save the value for the next function call
lastChosen=chosen;
}
If you want to get rid of dupes, you'll have to keep track of what the last item chosen was, and pick a new item if you pull a dupe. We could do this with a "do-while" loop.
This certainly makes it seem more random, even if it's technically not.
Should I just ignore the "Warning: local declaration of 'myArray' hides instance variable" messages." that I keep getting?
Thanks again for sharing your knowledge on this. It is a huge help. I really appreciate it.
Is that because you defined the array in your header file and your .m??
I'm not sure. I tried removing the statements related to myArray from the Controller.h file, but then it doesn't run at all. Here's what I have in there now.
Should I just ignore the "Warning: local declaration of 'myArray' hides instance variable" messages." that I keep getting?
In my code, I put
NSArray *myArray= blah blah blah.
That creates a new array pointer and points it at the array.
If you already have an array pointer, you can just say:
myArray= blah blah blah.
That'll use the array pointer you already have, and fix the warning you were getting. The message you got really means "You already have a variable myArray, and you made another variable called myArray? WTF?"
You can have two variables with the same name - one for the instance and one inside a method - but it's uncommon and can lead to coding mistakes; so the complier warns you
about it.
"You already have a variable myArray, and you made another variable called myArray? WTF?"
I am sorry to keep dragging this thread on, but I'd really like to learn what I'm doing wrong. I am still getting two "warning: local declaration of 'myArray' hides instance variable" errors.
I've tried various ways of renaming or removing statements, but am embarrassed to say I can't figure it out.
Here's all the myArray code in my ViewController.h file
I think you have your labels in your post reversed? Maybe you have it right in the files; the .h file as the interface and the property; the .m file has the synthesize and the implementation.
I am sorry to keep dragging this thread on, but I'd really like to learn what I'm doing wrong. I am still getting two "warning: local declaration of 'myArray' hides instance variable" errors.
I've tried various ways of renaming or removing statements, but am embarrassed to say I can't figure it out.
Here's all the myArray code in my ViewController.m file
I am also a new programmer and every time it runs, I get the same order of answers - I have put 6 values in the array and they appear - 6, 3, 5, 5, 6, 2, 3, 5, 2, 4 ......
I can close the sim, recompile and sure enough they appear in this order everytime.
I am also a new programmer and every time it runs, I get the same order of answers - I have put 6 values in the array and they appear - 6, 3, 5, 5, 6, 2, 3, 5, 2, 4 ......
I can close the sim, recompile and sure enough they appear in this order everytime.
Any ideas?
Yes - random() is actually a pseudorandom number generator. It starts with a "seed" value, and always generates the same set of numbers from that seed. Sooooo... the trick is to pick a different "seed" every time you start the program. The most common way to to do that is by using the current date+time as the seed; then you should get a different sequence every time.
Try this at the beginning of your program; say, in applicationDidFinishLaunching:
srandom (time (0));
BTW, don't get fancy about calling random() more than once, or multiplying two random number to get an even-more-random number, etc - you'd actually be removing randomness, not increasing it.
So to take this thread one step further (or back), I have random text in an array and it is working great.
I want to be able to simply hit a button or use a swipe gesture to go back in the array and see the text that was previously displayed.
How would you be able to navigate backwards in an array? If the text displays randomly, will the iPhone still remember what was previously displayed or will it just create a new random text string from the array?
If the text displays randomly, will the iPhone still remember what was previously displayed or will it just create a new random text string from the array?
If you take a look at post #5 in this thread, Smasher added some code to keep track of the last thing selected. It is for the purpose of not displaying the same response twice in a row, but you should be able to leverage it for your purposes.
Code:
//save the value for the next function call
lastChosen=chosen;
In general, you want to make an array of your (images / strings,) then pick a random number, then grab that Item from the array. Something like this:
Code:
//create the array
NSArray *myArray= [NSArray arrayWithObjects: @"Yes",@"No",@"File Not Found", nil];
//get the length of the array
int length = [myArray count];
//choose an item - C magic happens here :)
int chosen = (float)random() * length /RAND_MAX;
//get that item
NSString *item = [myArray objectAtIndex: chosen];
//print that item
NSLog(@"The item I picked is: %@", item);
That's 4 bits of cocoa magic, and one bit of plain-old C magic.
it works|||||but i want to display an images randomly.........how to display the image is my question........