# Thread: negative random numbers

1. ## negative random numbers

How Can I create negative random numbers?

Will this trick do the work ?

Code:
```int num = rand() % 10 +1;
int num2 = rand () % 10 +1;

int negRandNum = num * ( - num2 );```

2. Depending on what you actually want (your routine will never produce some integers within a range).

You can simply flip the sign of a variable: var = -var;

3. how to flip the sign

sry for the silly q but i rly d0nt kn0

4. Originally Posted by manzoor
sry for the silly q but i rly d0nt kn0
Sorry for the silly question, but I really don't understand.
Kurt

5. anon gave an example: var = -var;

6. so

Code:
```int num = rand() &#37; 10 + 1;
int negNum = -num;```

7. If you've got a random number between 0 and 9, what happens if you subtract 10?
That's simpler that adding 1 then flipping the sign.

8. i really dont care about the range of random numbers

just tell me whether im flipping the sign correctly or not /

9. Originally Posted by manzoor
i really dont care about the range of random numbers

just tell me whether im flipping the sign correctly or not /
I don't know. I would have to write a small program to test it.
Kurt

10. Originally Posted by manzoor
i really dont care about the range of random numbers

just tell me whether im flipping the sign correctly or not /
This is simple algebra, not calculus...
x * -1 = -x
-x * -1 = x

If you want a positive number to be negative, just multiply by -1.

11. The question is whether the code is correct.

It looks correct to me, but you should try it yourself to make sure it's working for you.

12. Originally Posted by manzoor
i really dont care about the range of random numbers

just tell me whether im flipping the sign correctly or not /
I'm guessing that rude reply was in response to my post. I don't care about the range either!
I was in fact showing you that through basic math you can simply subtract 10 INSTEAD of adding one and then flipping the sign.