-2

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?

1 Answers1

0
(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 ints then casting it into double.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
Sly_TheKing
  • 518
  • 7
  • 14