Hi, can someone tell me how to use rand() to generate a random bumber between two values. I want to write a function "int get_rand(int low, int high);" that will return a random number inbetween (not including) the two ints that are passed to it.
Here is what I have of it so far:
1 2 3
int get_rand(int low, int high) {
return ((rand() % high + 1) - low);
}
I'm getting other strange errors in my program so I haven't tested this function yet but I think it might work.
int randomRange( int _Min, int _Max )
{
//add 1 to the max, so it's included in the result.
_Max += 1;
int x = _Min + rand() % ( _Max - _Min );
return x;
}
You're welcome. I never liked writing code for random numbers, because I kept forgetting it. So now I have a folder with functions I use all the time, lol.