我正在尝试使用以下代码生成随机负 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/