<分区>
标签 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/