iOS开发中随机数的用法

778 阅读1分钟
1)、arc4random() 比较精确不需要生成随即种子
使用方法 :
通过arc4random() 获取0到x-1之间的整数的代码如下:
int value = arc4random() % x; 
获取1到x之间的整数的代码如下:
int value = (arc4random() % x) + 1; 

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)));

}

随机数产生的效果

#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:@"提示" message:@"点击屏幕暂停/开始流星陨落" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {}];
        [_alertVC addAction:okAction];
    }
    return _alertVC;
}

- (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)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    ///隐藏导航栏
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (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];
    }else{/**如果数组为空,那么创建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;
    }
}

@end