Int value = arc4random() % x; int value = arc4random() % x; Int value = (arc4random() % x) + 1; Int x = arc4random() % 100 int x = arc4random() % 100 Int y = (arc4random() % 501) + 500; Select a random integer from [from,to], including from, Include to -(int)getRandomNumber:(int)from to:(int)to {return (int)(from + (arc4random() % (to - from + 1)));}Copy the code

Random number effect

#import "ViewController.h" @interface ViewController () @property(nonatomic,strong) NSMutableArray *arrM; @property(nonatomic,strong) NSTimer *timer; @property(nonatomic,strong) UIAlertController *alertVC; @property(nonatomic,assign) BOOL onClick; @end @implementation ViewController - (UIAlertController *)alertVC { if (! _alertVC) {_alertVC = [UIAlertController alertControllerWithTitle: @ "prompt message:" @ "click on the screen to pause/start meteors fall" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction * okAction = [UIAlertAction actionWithTitle: @ "sure" style: UIAlertActionStyleDefault handler: ^ (UIAlertAction *  _Nonnull action) {}]; [_alertVC addAction:okAction]; } return _alertVC; } - (NSTimer *)timer { if (! _timer) {_timer = [NSTimer scheduledTimerWithTimeInterval: 0.5 target: self selector: @ the selector (snowShow) the userInfo: nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes]; } return _timer; } - (NSMutableArray *)arrM { if (! _arrM) { _arrM = [NSMutableArray array]; } return _arrM; } - (BOOL)prefersStatusBarHidden { return YES; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; / / / hide the navigation bar [self navigationController setNavigationBarHidden: YES animated: NO]; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; // call [self timer]; } - (void)snowShow { UIImageView *imageView = nil; int x = (int)self.view.bounds.size.width; Int speed = (arc4random() % 2) + 3; If (self.arrm. Count > 0) {imageView = [self.arrm firstObject]; [self.arrM removeObject:imageView]; }else{/** Create UIImageView**/ imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"map_loc"]]; [self.view addSubview:imageView]; } imageView.frame = CGRectMake(arc4random() % x , -50, 24, 24); [UIView animateWithDuration: speed animations: ^ {imageView. Alpha = 0.2;  imageView.frame = CGRectMake(arc4random() % x, self.view.bounds.size.height/4, 24, 24);  } completion:^(BOOL finished) { imageView.alpha = 1; [UIView animateWithDuration: speed animations: ^ {imageView. Alpha = 0.2;  imageView.frame = CGRectMake(CGRectGetMinX(imageView.frame), self.view.bounds.size.height -24, 24, 24);  } completion:^(BOOL finished) { imageView.alpha = 1; [self.arrM addObject:imageView]; }]; }]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { if (! self.onClick) { [self.timer invalidate]; self.timer = nil; self.onClick = YES; [self presentViewController:self.alertVC animated:YES completion:^{}]; }else{ self.onClick = NO; [self timer]; [self presentViewController:self.alertVC animated:YES completion:^{}]; } } - (void)dealloc { if (self.timer) { [self.timer invalidate]; self.timer = nil; } } @endCopy the code