hi im creating a app that generates random text when i press a button at the moment i have the code and it works fine but there are some things i would like to change when i run the app and i press the button it shows:
“alright, hiya, yoo, yoo, Hello, alright, Hiya, yoo, yoo, hiya, hello, hello, alright”
thats the first 13 clicks and thats the order every time i go on the app. Basically i dont want them to repeat twice in a row and also i want them to start in different orders when i start the app.
1 more thing i would like to be able to write at least 2 lines of text but how do i do that using a Label?
heres the code i have:
.h
@interface ViewController1 : UIViewController {
IBOutlet UILabel *textview;
}
-(IBAction)random;
.m
@interface ViewController1 ()
@end
@implementation ViewController1
-(IBAction)random {
int text = rand() % 5;
switch (text) {
case 0:
textview.text = @"Hello";
break;
case 1:
textview.text = @"hi";
break;
case 2:
textview.text = @"alright";
break;
case 3:
textview.text = @"yoo";
break;
case 4:
textview.text = @"hiya";
break;
default:
break;
}
}
thank you 🙂
Use the function
arc4random()instead ofrandom(). The problem you face is because the functionrandneeds a seed to be set before you call it. This is a starting value for the random number generator used in the background byrand. When you don’t use a own seed it’s always the same default, and you therefore always get the same sequence of random values. When usingarc4randomthere is no need for setting a seed. For further information see this blog post and the documentation.