Thread: negative random numbers

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    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. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    how to flip the sign


    sry for the silly q but i rly d0nt kn0

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by manzoor View Post
    sry for the silly q but i rly d0nt kn0
    Sorry for the silly question, but I really don't understand.
    Kurt

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    anon gave an example: var = -var;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    so

    Code:
    int num = rand() % 10 + 1;
    int negNum = -num;

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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"

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    i really dont care about the range of random numbers

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

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by manzoor View Post
    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. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by manzoor View Post
    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. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by manzoor View Post
    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.

    Besides, you already know the answer.
    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"

  13. #13
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 06:28 AM
  3. Using TextOut() to align positive & negative numbers
    By csonx_p in forum Windows Programming
    Replies: 4
    Last Post: 05-27-2008, 07:12 AM
  4. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  5. Generating 100k to 1 million unique random numbers
    By Ariod in forum C Programming
    Replies: 4
    Last Post: 08-26-2005, 12:59 PM