ios - arc4Random() - 生成数字 "jumping after 10"

标签 ios objective-c xcode random arc4random

我不确定如何设置 arc4Random() 的格式以生成 200 到 300 之间的随机数。我想要 200、210、220 等数字..(不是 200、201 , 202, ...) (Xcode 5.1.1, iOS)

有什么想法吗?

我的一段代码:

self.currentObstacX += arc4random()%(200+10) + 300;

...但它似乎无法满足我的需要。

提前谢谢你。

最佳答案

@PaulR 的答案会奏效,但最好像这样使用 arc4random_uniform...

NSInteger number = 10 * arc4random_uniform(11) + 200;

来自文档...

arc4random_uniform() is recommended over constructions like ``arc4random() % upper_bound'' as it avoids "modulo bias" when the upper bound is not a power of two.

关于ios - arc4Random() - 生成数字 "jumping after 10",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25849758/

相关文章:

ios - UINavigationBar 中的滑动手势

swift - NSTableView 获取具有单元格的 indexPath

ios - Realm 过滤器 CLLocation

ios - withinMiles 无法在 Swift 中使用 Parse PFGeopoint 查询

android - Stella SDK 转安卓

ios - 无法在一个 Storyboard 中显示两个选择器 View

iphone - 执行某些代码后如何为 UIButton 分配不同的选择器(取消之前的选择器方法)

ios - MSMessagesAppViewController didStartSending 重写方法不会在发送按钮点击时调用

Xcode Quick Help 在查看单个文件时显示 "No Quick Help"

iOS 约束不允许使用乘数