Randomly generate “-1” or “1” - the shortest method

I need to randomly generate "-1" or "1" to randomly determine the sign of a number ... What is the shortest method? I am currently using this, but it looks pretty long:

sign = (round((arc4random() % 2)))-((round((arc4random() % 2))) == 0);

+5
source share
2 answers

How about arc4random_uniform(2) ? -1 : 1?

or arc4random_uniform(2)*2 - 1

+12
source
short int randomNumber () {
return arc4random() % 2 ? 1 : -1;
}
+1
source

All Articles