samedi 19 mars 2016

rand() and RAND_MAX giving different values depending upon the datatype

Here when I print the value of x its giving zero as output.Whereas when I print y, I am getting correct value(a random number between 0 and 1),the typecasting is the problem it seems.Why do i need to typecast it?

double x,y;
x=rand()/RAND_MAX;
printf("X=%f\n",x);
y=(double)rand()/RAND_MAX;
printf("Y=%f",y);

Output

X=0.000000
Y=0.546745




Aucun commentaire:

Enregistrer un commentaire