I have made a code where when you tap on the image random text pops up from 1 of these cases. However, it does work, but very time you launch the application the text pops up in the same order.
Please help.
||Heres the code I use||
Code:
#import "Image.h"
@implementation Image
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
int randomNumber = rand() % 41; //change for amount of cases
switch (randomNumber) {
case 0:
textview.text = @"YOU WILL BE VISITED BY AN UNINVITED GUEST";
break;
case 1:
textview.text = @"NOBODY CAN HELP YOU NOW";
break;
case 2:
textview.text = @"JOY IS ALL AROUND YOU, YOU JUST HAVE TO LOOK";
break;
case 3:
textview.text = @"DANGER IS ON YOUR PATH";
break;
case 4:
textview.text = @"LOVE WILL FIND YOU, BUT WILL YOU CHOOSE TO ACCEPT IT?";
break;
case 5:
textview.text = @"NOTHING IS SET IN STONE";
break;
case 6:
textview.text = @"YOU WILL FIND A LOT OF JOY IN THE NEXT OF YOUR DAYS";
break;
case 7:
textview.text = @"IMPORTANT CHOICES LIE ON YOUR PATH";
break;
case 8:
textview.text = @"MONEY AND LUCK WILL COME SOON";
break;
case 9:
textview.text = @"GREAT SORROW WILL FIND YOU SOON";
break;
case 10:
textview.text = @"The leader seeks to communicate his vision to his followers.";
break;
case 11:
textview.text = @"There is always a way - if you are committed.";
break;
case 12:
textview.text = @"You will enjoy good health, you will be surrounded by luxury";
break;
case 13:
textview.text = @"Money will come to you when you are doing the right thing.";
break;
case 14:
textview.text = @"Do not hesitate to look for help, an extra hand is welcomed.";
break;
case 15:
textview.text = @"The Wheel of Good Fortune is finally turning in your direction!";
break;
case 16:
textview.text = @"You will be called to fill a position of high honor and responsibility";
break;
case 17:
textview.text = @"Keep your plans secret for now.";
break;
case 18:
textview.text = @"You will enjoy razon-sharp spiritual vision today.";
break;
case 19:
textview.text = @"It takes more than good memory to have good memories.";
break;
case 20:
textview.text = @"Money and Pride will find you";
break;
case 21:
textview.text = @"There will be a time of great depression, but followed with joy.";
break;
case 22:
textview.text = @"Your skill will accomplish what the force of many cannot.";
break;
case 23:
textview.text = @"You will make change for the better";
break;
case 24:
textview.text = @"The greatest danger could be your stupidity.";
break;
case 25:
textview.text = @"Don't face reality, let it be the place from which you leap.";
break;
case 26:
textview.text = @"You are one of the people who will go places.";
break;
case 27:
textview.text = @"The dream is within you.";
break;
case 28:
textview.text = @"A conclusion is simply the place where you got tired of thinking.";
break;
case 29:
textview.text = @"Love is for the lucky and the brave.";
break;
case 30:
textview.text = @"You should not seach for love, it will find you.";
break;
case 31:
textview.text = @"You will always be successful in your professional career.";
case 32:
textview.text = @"You will outdistance all your competitors.";
case 33:
textview.text = @"You are very grateful for the small pleasures of life.";
case 34:
textview.text = @"Oops... Wrong fortune.";
case 35:
textview.text = @"Your skill will accomplish what the force of many cannot.";
case 36:
textview.text = @"What ends on hope does not end at all.";
case 37:
textview.text = @"If you never expect anything you can never be disappointed.";
case 38:
textview.text = @"You will be sharing great news with all people you love";
case 39:
textview.text = @"Keep your eyes open. You never know what you might see.";
case 40:
textview.text = @"Use your instincts now.";
default:
break;
}
}
@end
Rand returns pseudo-random numbers determined by the initial seed value and the number of times rand has been called.
I believe that value is defaulted to 1. Try to seed it with something like the current time to get a more random effect.
Code:
srandom(time(NULL));
You may want to also check out arc4random() and see if that may fit your needs as well.
arc4random() generates much better random numbers, and is self-seeding. rand() has problems. Specifically, the lower bits of rand() are subject to repeating patterns, which makes it really bad in the typical use where you take the modulo of a random value.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
arc4random() generates much better random numbers, and is self-seeding. rand() has problems. Specifically, the lower bits of rand() are subject to repeating patterns, which makes it really bad in the typical use where you take the modulo of a random value.
Thanks, that solved the problem that I was having.
I have made a code where when you tap on the image random text pops up from 1 of these cases. However, it does work, but very time you launch the application the text pops up in the same order.
Please help.
||Heres the code I use||
Code:
#import "Image.h"
@implementation Image
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
int randomNumber = rand() % 41; //change for amount of cases
switch (randomNumber) {
case 0:
textview.text = @"YOU WILL BE VISITED BY AN UNINVITED GUEST";
break;
case 1:
textview.text = @"NOBODY CAN HELP YOU NOW";
break;
case 2:
textview.text = @"JOY IS ALL AROUND YOU, YOU JUST HAVE TO LOOK";
break;
case 3:
textview.text = @"DANGER IS ON YOUR PATH";
break;
case 4:
textview.text = @"LOVE WILL FIND YOU, BUT WILL YOU CHOOSE TO ACCEPT IT?";
break;
case 5:
textview.text = @"NOTHING IS SET IN STONE";
break;
case 6:
textview.text = @"YOU WILL FIND A LOT OF JOY IN THE NEXT OF YOUR DAYS";
break;
case 7:
textview.text = @"IMPORTANT CHOICES LIE ON YOUR PATH";
break;
case 8:
textview.text = @"MONEY AND LUCK WILL COME SOON";
break;
case 9:
textview.text = @"GREAT SORROW WILL FIND YOU SOON";
break;
case 10:
textview.text = @"The leader seeks to communicate his vision to his followers.";
break;
case 11:
textview.text = @"There is always a way - if you are committed.";
break;
case 12:
textview.text = @"You will enjoy good health, you will be surrounded by luxury";
break;
case 13:
textview.text = @"Money will come to you when you are doing the right thing.";
break;
case 14:
textview.text = @"Do not hesitate to look for help, an extra hand is welcomed.";
break;
case 15:
textview.text = @"The Wheel of Good Fortune is finally turning in your direction!";
break;
case 16:
textview.text = @"You will be called to fill a position of high honor and responsibility";
break;
case 17:
textview.text = @"Keep your plans secret for now.";
break;
case 18:
textview.text = @"You will enjoy razon-sharp spiritual vision today.";
break;
case 19:
textview.text = @"It takes more than good memory to have good memories.";
break;
case 20:
textview.text = @"Money and Pride will find you";
break;
case 21:
textview.text = @"There will be a time of great depression, but followed with joy.";
break;
case 22:
textview.text = @"Your skill will accomplish what the force of many cannot.";
break;
case 23:
textview.text = @"You will make change for the better";
break;
case 24:
textview.text = @"The greatest danger could be your stupidity.";
break;
case 25:
textview.text = @"Don't face reality, let it be the place from which you leap.";
break;
case 26:
textview.text = @"You are one of the people who will go places.";
break;
case 27:
textview.text = @"The dream is within you.";
break;
case 28:
textview.text = @"A conclusion is simply the place where you got tired of thinking.";
break;
case 29:
textview.text = @"Love is for the lucky and the brave.";
break;
case 30:
textview.text = @"You should not seach for love, it will find you.";
break;
case 31:
textview.text = @"You will always be successful in your professional career.";
case 32:
textview.text = @"You will outdistance all your competitors.";
case 33:
textview.text = @"You are very grateful for the small pleasures of life.";
case 34:
textview.text = @"Oops... Wrong fortune.";
case 35:
textview.text = @"Your skill will accomplish what the force of many cannot.";
case 36:
textview.text = @"What ends on hope does not end at all.";
case 37:
textview.text = @"If you never expect anything you can never be disappointed.";
case 38:
textview.text = @"You will be sharing great news with all people you love";
case 39:
textview.text = @"Keep your eyes open. You never know what you might see.";
case 40:
textview.text = @"Use your instincts now.";
default:
break;
}
}
@end
By the way, a giant case statement is a pretty painful way to do this. It's cleaner and easier to save your strings into an array, and then use the random number to index into the array:
Code:
self.stringsArray = [NSArray arrayWithObjects:
@"YOU WILL BE VISITED BY AN UNINVITED GUEST",
@"NOBODY CAN HELP YOU NOW",
@"JOY IS ALL AROUND YOU, YOU JUST HAVE TO LOOK",
@"DANGER IS ON YOUR PATH",
@"LOVE WILL FIND YOU, BUT WILL YOU CHOOSE TO ACCEPT IT?",
nil];
NSUInteger index = arc4random() %[stringsArray count]
textView.text = [stringsArray objectAtIndex: index;
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.