码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-07-07 17:19:14      阅读:131      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!