iOS开发中生成随机数

xiaoxiao2021-02-28  53

有时候我们需要在程序中生成随机数,但是在Objective-c中并没有提供相应的函数,好在C中提供了rand()、srand()、random()、arc4random()几个函数。那么怎么使用呢?下面将简单介绍:

1、 获取一个随机整数范围在:[0,100)包括0,不包括100

int x = arc4random() % 100;

2、 获取一个随机数范围在:[500,1000),包括500,包括1000

int y = (arc4random() % 501) + 500;

3、 获取一个随机整数,范围在[from,to),包括from,包括to

-(int)getRandomNumber:(int)from to:(int)to { return (int)(from + (arc4random() % (to – from + 1))); }

以上转载来自于http://www.cnblogs.com/daguo/archive/2013/05/30/3107824.html,尊重原创

以下,根据产生的随机数,制作雪花飘落/星星陨落的效果:

// // ViewController.m // MeteorShower // // Created by HZhenF on 2017/5/6. // Copyright © 2017年 Huangzhengfeng. All rights reserved. // #import "ViewController.h" @interface ViewController () @property(nonatomic,strong) NSMutableArray *arrM; @property(nonatomic,strong) NSTimer *timer; @property(nonatomic,strong) UIAlertController *alertC; @property(nonatomic,assign) BOOL onClick; @end @implementation ViewController -(UIAlertController *)alertC { if (!_alertC) { _alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"点击屏幕暂停/开始流星陨落" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; [_alertC addAction:okAction]; } return _alertC; } -(NSTimer *)timer { if (!_timer) { _timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(snowShow) 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)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; //主动调用一次 [self timer]; } -(void)snowShow { UIImageView *imageView = nil; int x = (int)self.view.bounds.size.width; //下落速度 int speed = (arc4random() % 2) + 3; /**如果有数组里面有UIImageView,直接取来用就行**/ if (self.arrM.count > 0) { imageView = [self.arrM firstObject]; [self.arrM removeObject:imageView]; } /**如果数组为空,那么创建UIImageView**/ else { imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"start"]]; [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.alertC animated:YES completion:^{ }]; } else { self.onClick = NO; [self timer]; [self presentViewController:self.alertC animated:YES completion:^{ }]; } } @end

转载请注明原文地址: https://www.6miu.com/read-75362.html

最新回复(0)