I'm trying to generate a random number between 1 and 0 for a school project. Several places say this should be done using rand() and casting to float. Something like the following:
float x = ((float)rand()/(float)(RAND_MAX)) * a;
However, I discovered that whenever I cast from int to float the number is incorrect. This issue is causing me to get incorrect answers using the code above
Code:
int rotlat = rand() % 1000;
double newrot = (double) rotlat;
printf("rotational latency: %d\n", rotlat);
printf("new rotational latency: %d\n", newrot);
Output:
rotational latency: 533
new rotational latency: -1841876320
rotational latency: 279
new rotational latency: -1841876320
rotational latency: 148
new rotational latency: -1841876320
rotational latency: 167
new rotational latency: -1841876320
Aucun commentaire:
Enregistrer un commentaire