Random Numbers within a range OTHER than 1-X

This is a discussion on Random Numbers within a range OTHER than 1-X within the C++ Programming forums, part of the General Programming Boards category; Hi there, I have been trying to find a way to generate a random number within a range other than ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    44

    Random Numbers within a range OTHER than 1-X

    Hi there,

    I have been trying to find a way to generate a random number within a range other than "1-X".

    For example, the user will enter the low extreme and the high extreme - 20 and 80 perhaps. I have come up with a solution, but it doesn't seem very efficient. It bounces the number around until it satisfies the range.

    I do have this working. What I'm wondering is if there is a better way to do it. Here's what I've got:

    Code:
    //l is for low extreme, h is for high extreme
    int gen_rand(int l, int h) {
        srand(time(NULL));
        int my_rand = rand()%h +1;
    
        // (+/- 1) only - determines whether or not the number will
        // be reduced or increased in order to satisfy the given range
        int sign = 1;
    
        // if my_rand is lower than l (low range), add more - but if it goes above
        // h, subtract some.. repeat until my_rand is within the bounds of l and h.
        while (my_rand > h || my_rand < l) {
            if (my_rand > h) sign = -1; // we need to bring it down
            else if (my_rand < l) sign = 1; // bring it up
            my_rand += ((rand()%h)*sign) +1;
        }
        return my_rand;
    }
    I am a noob. I've tried multiple approaches to this and actually am lucky I was able to come up with even this. What I'm asking is whether or not there is a standard way of doing this (a common algorithm used "all the time") that I'm just missing.

    I know that the code itself could probably stand some improvements, but I'm actually asking for help on the algorithm level, not the syntax level.

    Thanks,
    Kaelin

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    If you want a number within a specified range, you could just do this:

    Code:
    //get user input for min
    cin >> min;
    
    //get user input for max
    cin >> max;
    
    //get random number
    int rand_num = min + rand()%(max-min);
    Last edited by The Brain; 02-15-2005 at 06:39 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    You have a hi and lo number,
    Code:
    // subtract lo from hi (80 - 20 = 60)
    // generate number in range up to difference (60)
    random_num = rand() % (hi - lo);
    
    // then just add the lo
    random_num += lo;
    say number generated was 42 adding lo would make it 62
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  4. #4
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    For ease of comprehension, I typically refer to variables range and offset. offset being the minimum result.
    Code:
        int range;
        int offset;
        int num;
        
        num = rand() % range + offset;

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Code:
    int rand_in_range( int hi, int lo ) {
            srand(time(NULL));
    
            return ((rand() % (hi - lo)) + lo);
    }
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Is it possible to make a program that solves qaudratic equations?
    I know it you have to make a recursive loop for powers, but I have high a high class graphing calculator and it has problems with negative exponents. So my math teacher told me not to trust the calcs on this kind of stuff.
    My computer is awesome.

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    44
    Wow - after seeing the solution it's hard to believe I didn't get it in the first place. Thanks all, for your help

    Alas, my biggest problem with C++ - not the syntax, but the creative thinking.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Alas, my biggest problem with C++ - not the syntax, but the creative thinking.
    You can rest assured that most people didn't come up with that on their own--they read it in their beginning C++ text.

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    cant remember ever reading Beginning C++, now Beginning C yes, but cant remember last time I picked it up though.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by 7stud
    You can rest assured that most people didn't come up with that on their own--they read it in their beginning C++ text.
    you can't come up with
    Code:
    return ((rand() % (hi - lo)) + lo);
    on your own?

    to slove a quadratic equation:
    Code:
    #include<cmath>
    
    int main()
    {
        double a,b,c;
        //for positive:
        a=((-b+sqrt(pow(b,2)-(4*a*c)))/2.0*a);
        //for negative:
        a=((-b-sqrt(pow(b,2)-(4*a*c)))/2.0*a);
    }
    you may want to look that over... I pumped it out real fast without double-checking...




    edit: I'm guessing it's 7stud that likes to leave bad rep without a comment OR name...
    Last edited by major_small; 02-16-2005 at 02:40 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Registered User
    Join Date
    Feb 2005
    Posts
    44
    ^^^
    ???

    To be honest, I'm not really sure where that post came from. I don't think there's any need to insult somebody for forgetting "the dreaded quadratic equation".

    I know that some of us after high school made an oath never to use it again.. [laughs]

    Though no, it wasn't me who reported on your post. I tend to leave comments.

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Kaelin
    To be honest, I'm not really sure where that post came from. I don't think there's any need to insult somebody for forgetting "the dreaded quadratic equation".
    that slight hostility came from
    Quote Originally Posted by 7stud
    You can rest assured that most people didn't come up with that on their own--they read it in their beginning C++ text.
    (no hard feelings)
    and the quadratic bit came from
    Quote Originally Posted by cerin
    Is it possible to make a program that solves qaudratic equations?
    I know it you have to make a recursive loop for powers, but I have high a high class graphing calculator and it has problems with negative exponents. So my math teacher told me not to trust the calcs on this kind of stuff.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

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. random number in range
    By lgg in forum Linux Programming
    Replies: 3
    Last Post: 08-14-2005, 05:15 AM
  3. Random Numbers (Advanced)
    By misplaced in forum C++ Programming
    Replies: 11
    Last Post: 03-25-2005, 02:57 AM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. random double numbers
    By Gugge in forum C Programming
    Replies: 7
    Last Post: 05-17-2002, 11:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21