ios - Objective-C 中的随机数

标签 ios objective-c random

<分区>

Possible Duplicates:
Generating random numbers in Objective-C
iOS Random Numbers in a range
Generate non-repeating, no sequential numbers

我正在寻找在两个数字之间给我随机数的方法,第一个数字将是我在两个数字之间选择的数字。

例如,如果我给这个随机函数 5、10 和 9 作为第一个数字,那么它会给我:9,10,7,6,5,8

我尝试使用它:

NSUInteger count = [array count];
for (NSUInteger i = 1; i < count; ++i) {
        int nElements = count - i;
    int n = (random() % nElements) + i;
    while (n==`firstnumber`) {
        n = (random() % nElements) + i;
    }
}

最佳答案

int r = arc4random() % 9 + 5 将为您提供 5 到 13 之间的数字,包括这两个数字。

关于ios - Objective-C 中的随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6993905/

上一篇:java - 将 Apple 的 Cocoa 框架 Epoch (reference_date) 转换为 Java Date

下一篇:objective-c - 分配和释放问题

相关文章:

c++ - 具有动态位置的 body 节点形状

ios - 单击推送通知自定义按钮时如何打开 iOS 应用程序?

ios - NSExtensionContext openURL 不起作用

ios - 类不会填充在 Identity Inspector Xcode 中

ios - NSInvalidArgumentException',原因 : 'Application tried to present a nil modal view controller on target

ios - 从应用程序委托(delegate)中推送 ViewController

opencv - 在 OpenCV 中生成随机数矩阵

iphone - Objective-C 中的问题随机数

ios - 如何在 RxSwift 中使用数据源和委托(delegate)方法

ios - 具有圆形边框的 tableview xcode swift