Thread: Random number from -10 to 10?

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Random number from -10 to 10?

    How do I do this? I tend to use the simple method (for positive integers), which I've heard is a very poor choice, but I don't know how to combine it with negative values.

    Code:
    rand() % 10 + 1

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    68
    Code:
    rand()%21 - 10

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,834
    Hawkin pretty much gave you the answer that you were looking for, but also read Prelude's article on using rand().
    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

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random number guessing game
    By kazoo in forum C Programming
    Replies: 7
    Last Post: 05-30-2010, 11:31 AM
  2. random number gen & looping probelm :?
    By FoxeySoulSLayer in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2009, 05:44 PM
  3. How get 10 random number
    By aminpost in forum C++ Programming
    Replies: 12
    Last Post: 02-26-2009, 03:57 AM
  4. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM