mercredi 19 juillet 2017

c++ generating a double between 2 doubles

So I need to randomly generate a double between 2 doubles, and in this specific case, it is to randomly generate a GPA. I tried at first to do this:

srand(time(NULL));

double rands1 = rand() % (4) + ((rand() % (10)) / 10.0);

And this does work... mostly. I can get anywhere from a 0.0 to 3.9, but it has to be from 0.0 to 4.0 inclusive! Changing the 4 in the code to a 5 can give 4.1 and higher as well however, and that is no good.

My question is, what would be a more efficient way to code this? Thank you!




Aucun commentaire:

Enregistrer un commentaire