ios - 生成一个随机的负 NSInteger

标签 ios objective-c random nsinteger

我正在尝试使用以下代码生成随机负 NSInteger,

+ (NSInteger)getNegativeRandomNumber {
    uint32_t random = arc4random_uniform(2147483647); // where 2147483647 is the max 32-bit positive value returned by random function
    return (NSInteger)(-1 * random); // convert the positive into negative number
}

但偶尔这段代码会给我正数。您看到这段代码有什么问题吗?

最佳答案

你得到负数

return -(NSInteger)random;

您需要首先转换为签名类型。

关于ios - 生成一个随机的负 NSInteger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42576676/

相关文章:

ios - 如何使用 AVPlayer 无延迟地播放音频

ios - 实现 UIApplicationDelegate 协议(protocol)的窗口属性

objective-c - 更快的 if 语句 : if `variable` is "value" or "value"

ios - 透明背景 UIView drawRect 圆形

ios - swift ios 一遍又一遍地更改 uilabel 文本值

iphone - 即使应用程序未运行,是否也可以跟踪位置

ios - 在 Objective-C 中读取 rtf 文件

javascript - Node.js 正在捉弄我。使用数组生成随机名称和推文

python - 在 Python 中随机化字符串列表的最佳方法

java - 偶数和奇数的随机数生成器