It works fine:
srand(time(NULL));
cout<<(double)rand()/RAND_MAX<<endl;
It doesn't work:
srand(time(NULL));
cout<<static_cast<double>(rand()/RAND_MAX)<<endl;
Why? How to fix it?
It works fine:
srand(time(NULL));
cout<<(double)rand()/RAND_MAX<<endl;
It doesn't work:
srand(time(NULL));
cout<<static_cast<double>(rand()/RAND_MAX)<<endl;
Why? How to fix it?
(double)rand()/RAND_MAX
Here you cast only rand()
number. So in your code it should be like that too:
static_cast<double>(rand())/RAND_MAX
In your example you were dividing two int
s then casting it into double
.