ios - arc4random() 在 iOS 中给出负数

标签 ios objective-c arc4random

有时 arc4random() 在 Objective-C 中也会给出负数。

我的代码如下:

尝试 1:

long ii = arc4random();

尝试 2:

int i = arc4random();

如何只得到正数随机数?

谢谢,

最佳答案

不,它总是正的,因为它返回一个无符号的 32 位整数 (manpage):

u_int32_t arc4random(void);

您将其视为有符号整数,这是不正确的。

关于ios - arc4random() 在 iOS 中给出负数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36447071/

相关文章:

objective-c - 一般 CoreData 性能

ios - 在 UITableView 中的 UINib 中重新加载 UITableView

iphone - 使用渐变和阴影创建按钮的单独方法

ios - 亚马逊 AWS SNS : How do i subscribe to SNS topic from iOS?

Swift 在 0 和 1 之间随机 float

iphone - -[__NSArrayM objectAtIndex :]: index 4294967295 beyond bounds for empty array with arc4random

通过附加两个 int 的二进制表示来创建 double

ios - 加载消息时滚动到 UITableView 的底部

objective-c - 使用自定义对象对 NSArray 进行排序

ios - 尝试连接我的真实设备时出现 Appium 错误代码 253