mardi 29 janvier 2019

not getting random values even with srand(time(NULL))

Even after including srand(time(NULL)) at the start of my function, (function is only called once in main) I get the same random value for r1 every time I run the program. r2 and r3 get random values fine, but I need random decimal values between 0.1 and 10.0 so what's wrong with the line containing r1?

void randNums(float &r1, float &r2, float &r3) {

   srand(time(NULL));

   r1 = (10 * (rand())/ (float)RAND_MAX);
   r2 = 1 + (rand() % 10);
   r3 = 1 + (rand() % 10);

}




Aucun commentaire:

Enregistrer un commentaire