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 );
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;
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
how to flip the sign
sry for the silly q but i rly d0nt kn0
anon gave an example: var = -var;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
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.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
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