ios - 使用 arc4random() 时如何选择值的范围

标签 ios objective-c arc4random

我可以在使用 arc4random() 时设置一个数字范围吗?仅例如 50-100。

最佳答案

正如下面其他帖子中所指出的,最好使用 arc4random_uniform。 (最初编写此答案时, arc4random_uniform 不可用)。除了避免 arc4random() % x 的模偏差之外,它还避免了 arc4random 在短时间内递归使用时的播种问题。

arc4random_uniform(4)

将生成 0、1、2 或 3。因此您可以使用:

arc4random_uniform(51)

只需将 50 添加到结果中即可获得 50 和 100(含)之间的范围。

关于ios - 使用 arc4random() 时如何选择值的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3420581/

相关文章:

ios - 子类化 UIView 需要什么

iphone - 从 NSObject 类获取 NSMutableArray 到 UIViewController 类时的内存管理

swift - 在 swift 中创建随机数数组的最短代码?

ios - Swift 如何从两个文本字段中获取 Int 并生成随机数

swift - 随机选择 skspritenode 节点生成 5 个 Sprite

ios - 找出用户在 iOS 显示的 UIAlertView 上选择了哪个按钮的方法,请求允许应用程序接收推送通知的权限

ios - Restkit:处理动态嵌套属性只返回一个对象

ios - PFFile 到 PFImageView

iphone - 如何根据 objective-c 中的内容设置UITextView的高度

ios - 解析 : how to design the data structure so that showing 500K followers will be fast