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 );
Printable View
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 );
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;
how to flip the sign
sry for the silly q but i rly d0nt kn0
anon gave an example: var = -var;
so
Code:int num = rand() % 10 + 1;
int negNum = -num;
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.
i really dont care about the range of random numbers
just tell me whether im flipping the sign correctly or not /
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.
oh no sorry iMalc, I didn't meant to be rude 2 u
I just wanted 2 know whether the code is correct or not
Thanks 4 your help
i got it