【发布时间】:2012-08-26 19:18:40
【问题描述】:
我正在使用arc4random() 生成一定范围内的数字。但是,我会间歇性地收到负数。我没有做任何奇怪的事情:
if([aBug x] == -1) {
x = arc4random() % columns;
}
if([aBug y] == -1) {
y = arc4random() % rows;
}
在这里,x 和 y 有时会设置为负值。 x 和 y 都是 int 类型。
【问题讨论】:
-
这里可能不重要,但一般建议使用
arc4random_uniform(columns)。来自文档:arc4random_uniform() 建议在arc4random() % upper_bound之类的结构上使用,因为当上限不是 2 的幂时,它可以避免“模偏差”。
标签: objective-c gnustep arc4random