# Random Numbers within a range OTHER than 1-X

• 02-15-2005
Kaelin
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
• 02-15-2005
The Brain
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);```
• 02-15-2005
bigtamscot
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
• 02-15-2005
Scribbler
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;```
• 02-15-2005
bigtamscot
Code:

``` int rand_in_range( int hi, int lo ) {         srand(time(NULL));         return ((rand() % (hi - lo)) + lo); }```
• 02-15-2005
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.
• 02-15-2005
Kaelin
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. :p
• 02-15-2005
7stud
Quote:

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.
• 02-15-2005
bigtamscot
cant remember ever reading Beginning C++, now Beginning C yes, but cant remember last time I picked it up though. :)
• 02-15-2005
major_small
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);`

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...
• 02-16-2005
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".

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.
• 02-16-2005
major_small
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.