Xcode: Random label generator no repeat? -


i creating app has random label generator, i'm finding repeating sometimes. , wondering if can take problem right out, i'm not sure how to? appreciated. i've provided .h , .m files. here's .h

#import <uikit/uikit.h> #import "messageui/messageui.h" #import "social/social.h" #import "accounts/accounts.h" #import <iad/iad.h>   @interface viewcontroller2 : uiviewcontroller <mfmessagecomposeviewcontrollerdelegate,    adbannerviewdelegate> { slcomposeviewcontroller *myslcomposersheet; //label being randomly generated. iboutlet uilabel *label;  }  -(ibaction)randombutton; -(ibaction)randombutton2; -(ibaction)sendsms:(id)sender; -(ibaction)posttofacebook:(id)sender; - (ibaction)sendatweet:(id)sender;  @end 

here's .m

-(ibaction)randombutton { int randomtext = rand() %151; switch (randomtext) {     case 0:         label.text = @"1";         break;     case 1:         label.text = @"2";         break;     case 2:         label.text = @"3";         break;     case 3:         label.text = @"4";         break;     case 4:         label.text = @"5";         break;     case 5:         label.text = @"6";         break;     case 6:         label.text = @"7";         break;     case 7:         label.text = @"";         break;     case 8:         label.text = @"8";         break;     case 9:         label.text = @"9";         break;     case 10:         label.text = @"10";         break;     case 11:         label.text = @"11";     //ect..... 

thanks in advanced.

your random number repeat @ point of time, can store them , check. 1 number till 150 filled, reset , start again.

you can this:

-(ibaction)randombutton {     nsinteger random=arc4random()%151;     if ([self.randomnumbers containsobject:@(randomnumbers)]) {         [self randombutton];     }     else{         [self.randomnumbers addobject:@(random)];     }     label.text=[nsstring stringwithformat:@"%d",random+1]; } 

here, randomnumbers property of type nsmutablearray. while reset make empty.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -