随机数生成 arc4random_uniform(_:UInt32) ->UInt32
2016-03-16 15:14
369 查看
随机数生成 arc4random_uniform(_:UInt32) ->UInt32
例子: let dicefacecount: UInt32 = 6
var test = Int(arc4random_uniform(dicefacecount))
print(test)
//生成0~5的数字
//不使用arc4random,因为在苹果5或者一下设备中,有时候会崩溃,请注意!是『有时候』!
//最佳实践如下:
func randomInRange(range: Range<Int>) -> Int {
let count = UInt32(range.endIndex-range.startIndex)
return Int(arc4random_uniform(count)) + range.startIndex
}
使用: print(randomInRange(1...6)) //输出1~6的任一数字
例子: let dicefacecount: UInt32 = 6
var test = Int(arc4random_uniform(dicefacecount))
print(test)
//生成0~5的数字
//不使用arc4random,因为在苹果5或者一下设备中,有时候会崩溃,请注意!是『有时候』!
//最佳实践如下:
func randomInRange(range: Range<Int>) -> Int {
let count = UInt32(range.endIndex-range.startIndex)
return Int(arc4random_uniform(count)) + range.startIndex
}
使用: print(randomInRange(1...6)) //输出1~6的任一数字
相关文章推荐
- 堵塞队列--BlockingQueue
- Ngui 左右滑动
- 解决的方法:warning: Clock skew detected. Your build may be incomplete.
- Xcode中的Version和Build的区别
- 导航栏颜色及UIBarButtonItem 的设置
- iOS This certificate has an invalid issuer
- 给UI控件画边框
- 从keyPressEvent按键事件中获取QKeySequence
- iOS开发中使用UIScrollView实现无限循环的图片浏览器
- mysql中key 、primary key 、unique key 与index区别
- UITableViewCell滑动删除及移动
- Swfit UITableView的使用(六)
- UICollectionView Section 纯代码代码控制 HeaderView和FooterView的显示,uicollectionview
- StringBuilder和String的区别
- Activity.runOnUiThread(Runnable)简单介绍
- uva 11195 Another queen (用状态压缩解决N后问题)
- php中使用$_REQUEST需要注意的一个问题
- Django View Request和Response
- iOS之UIScrollview添加约束图文详解
- 225. Implement Stack using Queues