首页 > 编程语言 > 详细

Swift - 使用arc4random()、arc4random_uniform()取得随机数

时间:2016-07-07 17:17:47      阅读:721      评论:0      收藏:0      [点我收藏+]

arc4random()这个全局函数会生成9位数的随机整数

1,下面是使用arc4random函数求一个1~100的随机数(包括1和100)

let temp:Int = Int(arc4random()%100)+1
        print(temp)

2,下面是使用arc4random_uniform函数求一个1~100的随机数(包括1和100)

let temps:Int = Int(arc4random_uniform(100))+1
        print(temps)

arc4random_uniform会随机返回一个0到上界之间(不含上界)的整数。以2为上界会得到0或1,像投硬币一样

 

原文链接:http://www.hangge.com/blog/cache/detail_513.html

Swift - 使用arc4random()、arc4random_uniform()取得随机数

原文:http://www.cnblogs.com/hero11223/p/5650377.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!