Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-04-2012, 11:06 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2011
Age: 14
Posts: 23
14DEV is on a distinguished road
Default Random Text

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
14DEV is offline   Reply With Quote
Old 02-04-2012, 11:47 AM   #2 (permalink)
Registered Member
 
Join Date: Jan 2012
Location: Illinois
Posts: 44
GHuebner is on a distinguished road
Default

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.
GHuebner is offline   Reply With Quote
Old 02-04-2012, 11:50 AM   #3 (permalink)
Registered Member
 
Join Date: Dec 2011
Age: 14
Posts: 23
14DEV is on a distinguished road
Default

Quote:
Originally Posted by GHuebner View Post
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.
Thanks for your help.
14DEV is offline   Reply With Quote
Old 02-04-2012, 12:32 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default use arc4random instead of rand

Quote:
Originally Posted by GHuebner View Post
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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-05-2012, 04:58 PM   #5 (permalink)
Registered Member
 
Join Date: Dec 2011
Age: 14
Posts: 23
14DEV is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
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.
14DEV is offline   Reply With Quote
Old 02-05-2012, 06:41 PM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by 14DEV View Post
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;
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 402
18 members and 384 guests
Apptronics RBC, Atatator, chiataytuday, dre, FrankWeller, gwelmarten, ipodphone, jeroenkeij, jleannex55, kukat, LunarMoon, MAMN84, n00b, pbart, reficul, Retouchable, Sami Gh, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,124
Posts: 402,909
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:13 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0