Hey guys, im new here and fairly new to c++, Im looking for some help on how to modify my random function so that i can input min and max values. I have tryed looking online and I have found several functions for random numbers. I chose not to use them as they seem complex and I didnt understand how they worked, (i dont like cheating).
int randomgen()
{
rand();
int random, max_value=10;
random = rand()/ (RAND_MAX / max_value + 1);
return random;
}
In my above function the max value set for the random number gen is 10, but i cant figure out how to add a minimum value, I need something like min value starting at 5, ending at 10.
Sorry to be a pest, but if you do help can you please explain the code?
int randomgen(int max, int min) //Pass in range
{
srand(time(NULL)); //Changed from rand(). srand() seeds rand for you.
int random = rand() % max + min;
return random;
}
Just take input as max and min then pass that into this function.
thanks for the reply, i added my twist to your code and it seems to work, however i was wondering if there is a better way of doing it. I first tryed this:
1 2 3 4 5 6 7
int randomGen()
{
rand();
int random, max_value = 10, min_value = 5;
random = rand() % max_value + min_value;
return random;
}
but this was returning values from 5 to 14.
so i changed it to this:
1 2 3 4 5 6 7
int randomGen()
{
rand();
int random, max_value =5, min_value = 5;
random = rand() % max_value + min_value;
return random;
}
and this is doing what i want, returning values in the range 5-9. just wondering if there is a better alternative as this looks fairly confusing, min is set to 5, max is at 5, and its displaying 5-9.
Hmm seems like strange results you got. I just had to write a random number generator myself for some reference strings on a page-replacement algorithm and used what I gave you. What compiler are you using? And that first line is supposed srand(/* some seed value */) still. Having rand() there does nothing.
Ah found the issue. It's a little different when you're not using 0 as your floor. I haven't messed with this in awhile and setting rand() ranges always confuse me.
rand()%(max-min)+min; will give you random numbers within a range of min - max.
This function is being used solely as a random generator that has limits set to it,
my main function is calling this and displaying the number it gave on screen.
my main function contains the srand( (unsigned)time( NULL )); along with other stuff that i cannot share online.
im using visual studio 2010, win 32 console app.
Thanks for the help I really do appreciate it, was looking at a simple way to set limits to random for some time.
If you have another method of performing the same task but looks less confusing I would be delighted to see it
That's the best way I've seen, and it's not that confusing. The math of it I don't understand, to be honest. Maybe someone can jump in and explain? :)
But the code is easy to follow.
that's because you're adding 5 + 5, then subtracting that (%) from rand() until it is less than 5. since rand starts from 0, and it has to be less than 5.
rand() picks randomly. number1 % number2 subtracts number 2 from number 1 until number 1 is greater than -1, but less than number 2. (values are from 0-number2).
So, rand() % 5 + 5 = (rand() % 5) + 5. so, it picks a number, modulates it less than 5, then adds 5.
if you want it to include 10, than simply add 1:
x = rand() % 11
while(x < 5)
{
x = (x + 6); (the amount of space in between min and max, 10 - 4, because we include 5)
}
because even though we add 5, our number can be anywhere between numbers 0 and 10. so, if it is 10, and we add 5, we will get 15. So we add an while statement to compensate. It basically does the opposite of modulo.
rand() % x gives you values in the range 0 to x-1.
If your randomgen function is defined as follows:
1 2 3 4
int randomgen(int min, int max)
{
return rand()%max + min ;
}
You return a random number in the range of 0 to max-1 plus the min value. So if max is 10 and min is 5, you get a value in the range of 5 to 14. What you need is a way to specify the range of values between min and max. max-min seems like a good place to start.
1 2 3 4 5
int randomgen(int min, int max)
{
int range = max-min ;
return rand() % range + min ;
}
So, let's consider again what happens when we feed this function the values 5 and 10. range is 5, oviously, so rand()%range gives us values in the range 0 to 4, and that will give us return values ranging from 5 to 9. Still, not quite what we want.
The astute reader may have noticed that max-min doesn't actually give us the range of [max,min]. It gives us the distance between the two. For instance, if we consider a range that includes only 5 to 7, the difference between min and max in this case is 2. But how many numbers are in the range of 5 to 7? 5, 6 and 7. 3 values. So the range that includes max and min is the distance between them plus 1, giving us the correct implementation of randomgen:
1 2 3 4 5
int randomgen(int min, int max)
{
int range = max-min+1 ;
return rand()%range + min ;
}
EDIT: Thank you everyone for your help, couldnt have done it without you guys
using this one (thanks to cire) because it seems the simplist to explain:
1 2 3 4 5 6
int randomGen()
{
int max = 10, min = 5, range;
range = max-min + 1;
return rand()%range + min;
}