jeudi 15 avril 2021

How to get a float random number in C with a range? [duplicate]

I am learning C and I am trying to get a random float number between 10 and 20. This is what I have so far:

  srand(time(NULL));
  float x = (float) rand() / (float) (RAND_MAX) * 10 + 20;
  printf("%f\n", x );

Whenever I run it only gives me a number that's something like so 20.103979. What am I doing wrong and how can I correct it?




Aucun commentaire:

Enregistrer un commentaire