有时候我们需要在程序中生成随机数,但是在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

iOS开发中生成随机数相关推荐

  1. ios 中生成随机数

    ios 中生成随机数 ios 有如下三种随机数方法: 1.    srand((unsigned)time(0));  //不加这句每次产生的随机数不变         int i = rand() ...

  2. iOS开发中常用的方法

    iOS开发中常用的方法 系统弹窗: 过期方法: UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"确认报价" ...

  3. ios 开发中 动态库 与静态库的区别

    使用静态库的好处 1,模块化,分工合作 2,避免少量改动经常导致大量的重复编译连接 3,也可以重用,注意不是共享使用 动态库使用有如下好处: 1使用动态库,可以将最终可执行文件体积缩小 2使用动态库, ...

  4. java 随机数生成实现_Java中生成随机数的实现方法总结

    搜索热词 在实际开发工作中经常需要用到随机数.如有些系统中创建用户后会给用户一个随机的初始化密码.这个密码由于是随机的,为此往往只有用户自己知道.他们获取了这个随机密码之后,需要马上去系统中更改.这就 ...

  5. iOS开发UI篇—iOS开发中三种简单的动画设置

    [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要"参与到"动画中 [UIView beginAnimations: ...

  6. IOS开发中单例模式使用详解

    第一.基本概念 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例类的特殊类.通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问. 第二.在IOS中使用单例模式的情 ...

  7. iOS开发中经常用的实用代码合集

    iOS开发中经常用的实用代码合集 本文整理了,在iOS开发中我们所遇到一些开发问题的技巧类的代码,让你在开发过程中避免了很多弯路,希望能给你的开发带来帮助和启发. 1.判断邮箱格式是否正确的代码: / ...

  8. iOS开发中的单元测试(三)——URLManager中的测试用例解析

    本文转载至 http://www.cocoachina.com/cms/plus/view.php?aid=8088   此前,我们在<iOS开发中的单元测试(一)&(二)>中介绍 ...

  9. iOS开发中静态库制作 之.a静态库制作及使用篇

    iOS开发中静态库之".a静态库"的制作及使用篇 一.库的简介 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.库的类型? 根据源代码的公开情况,库可以分为2种类 ...

最新文章

  1. c语言程序设计试题及答案十,C语言程序设计试题试题及答案.doc
  2. 堡垒机高危命令正则表达式
  3. NSAutoReleasePool使用中drain和release的区别
  4. c++函数返回值与引用
  5. Centos 7 修改主机名
  6. 用nginx TCP反向代理作mail邮件代理
  7. Visual C# 2008+SQL Server 2005 数据库与网络开发--13.1.1 菜单创建
  8. 惊心动魄的句子、帅帅酷酷的话
  9. 今天咱们用 Python 整一个 俄罗斯方块 小游戏吧(附源代码)
  10. html标签中的lang属性
  11. python对文件的写作方法_python读写csv文件 - 作文写作问答 - 归教作文网
  12. sql server 2016不能全部用到CPU的逻辑核心数的问题
  13. StringTokenizer是什么
  14. zynq系列之-----PS端iic使用
  15. GLES2.0中文API-glCopyTexSubImage2D
  16. 研发人员绩效评价常见误区
  17. 如何留住优秀的测试人员
  18. python+django加载静态网页模板
  19. html5支持4k视频,【4K电影大礼包】目前压缩最好的五部4KHEVC(H.265)格式电影
  20. Python程序——有一段英文文本,其中有单词连续重复了2次,编写程序检查重复的单词并只保留一个。

热门文章

  1. 探讨标准的采购外协“Subcontract”流程(一)
  2. Java实战教程:《iHRM 人力资源管理系统》
  3. iverilog 提示 Unknown module type 解决办法
  4. PYTHON使用arcpy出现“Error 000824: The tool is not licensed”
  5. 机器学习入门与Python实战(三):分类与逻辑回归 Logistic Regression
  6. 文正·高等数学每日一题(1)·极限
  7. 那些前端的特效(装哈哈神器)
  8. XCTF re5-packed-movement
  9. 音乐制作软件Logic Pro X for Mac
  10. 一次模拟餐馆的简单尝试