Thread: Quick Question

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    61

    Quick Question

    Hi There,
    Quick question i need a way of getting a random number in the range of 15 to 88.
    Im using visual studio.
    Does anyone know of a way of doing this

    thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you know of a way of getting a random number (integer?) in the range of 0 to 73?
    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

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    61
    no not with a range

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, 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

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Code:
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
        int result;
        srand(time(NULL));      /*  Do this once and only once before calling  rand */
    
        result = (rand() % (88-15)) + 15;   /*   Do this once each time you want a new random value */
    }
    Note where the values from your range come in. rand() produces a value in the range [0, RAND_MAX].

    This technique does not necessarily give a desirable distribution, but you haven't specified any such constraint.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    It should be rand() % 74 + 15 if you want 15 to 88 inclusive.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One quick question...
    By Kross7 in forum C++ Programming
    Replies: 10
    Last Post: 04-13-2007, 09:50 PM
  2. Quick question?
    By gvector1 in forum C++ Programming
    Replies: 3
    Last Post: 02-18-2003, 04:38 PM
  3. A quick question(s)
    By EvBladeRunnervE in forum C++ Programming
    Replies: 3
    Last Post: 02-17-2003, 09:39 PM
  4. quick question
    By ssjnamek in forum C++ Programming
    Replies: 17
    Last Post: 01-15-2003, 12:51 PM
  5. Quick Question.
    By SavesTheDay in forum C Programming
    Replies: 3
    Last Post: 04-23-2002, 10:09 AM