# generating random number in the range +/-(n to n+x)

I want to generate a random number that falls in the range 50 to 100 and -50 to -100 I am now using the following formula to achieve this:

(50 + (rand() % 50)) * ((rand() % 2) * 2 - 1)


where

(50 + (rand() % 50)) gives me a random number in the range 50 to 100 and
((rand() % 2) * 2 - 1) gives me +/- 1


I used two random generation calls, but Is it possible using just 1 random function call ? Are there any other better alternatives to the above formula ?

-

Assuming rand() gives a random on $(0,1)$, you can use x=rand(), if x < 0.5, y=100x-100, else y=100x
I assume you want a integer random number in $[-100,-50] \cup [50,100]$. I also assume that the function rand() gives a random integer in the range $[0,\text{RANDMAX}]$, where $\text{RANDMAX}$ is a large integer constant.
Get a random number in the interval [0,101] i.e. a = rand()%102