我需要随机生成数字的正负号,要么是“-1”,要么是“1”...有没有更短的方法?我目前在使用以下代码,但它看起来很冗长:
sign = (round((arc4random() % 2)))-((round((arc4random() % 2))) == 0);
我需要随机生成数字的正负号,要么是“-1”,要么是“1”...有没有更短的方法?我目前在使用以下代码,但它看起来很冗长:
sign = (round((arc4random() % 2)))-((round((arc4random() % 2))) == 0);
static const int tmp[] = {-1, 1}; return tmp[arc4random_uniform(2)];
- twalberg