lundi 7 janvier 2019

How do I generate a random double between 0 and max (in C)?

Here's what I did to generate the random double:

srand(time(NULL));

double max = 10.0;
double x = (double)rand()/(double)(RAND_MAX/max);

printf("The random number is %f \n", x);

Although the number this generates is random in nature, it's still not random enough.

The first time I ran this, I got 7.303385 Then I got 7.320475. Then I got 7.332377. Then I got 7.345195.

You get the idea. It look like my code is only generating random numbers between 7.3 and 7.4.

What am I doing wrong here?




Aucun commentaire:

Enregistrer un commentaire