Hey im trying to make a function that you can pass two values to, a RANGE_MIN and a RANGE_MAX, than generate and return a random number between those two numbers.
My first section of code repeats the same number every time i run the program (unless i change something and recompile)
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int getRand()
{
srand( (unsigned)time( NULL ) );
int RANGE_MIN = 0;
int RANGE_MAX = 100;
int myRand = (((double) rand() /
(double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
return myRand;
}
int main(){
cout << getRand();
cin.ignore(2);
}
and if i slightly modify that code and remove the calculations from the myRand integer and simply replace the calculations (((double) rand() / (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
with rand();
i am fine.
So it must have something to do with my double types losing precision or something like that?
EDIT: I figured out a much easier way of calculating a rand num that didnt involve all the typecasting (rand()%RANGE_MAX)+RANGE_MIN;