mercredi 22 juillet 2020

Why won't std::uniform_real_distribution generate proper floating values?

I'm trying to print random float(32-bit) values. I tried using uniform_real_distribution for this purpose. I wrote the following code,

int main()
{
  std::random_device rd{};
  std::mt19937 gen{rd()};

  std::uniform_real_distribution<float> dist(-1e18,1e18);

  float random_val = dist(gen);

  printf("%.20f\n", random_val);

  return 0;
}

Now, the output is strange. All I get is very big numbers (always near the upper or lower bound) with no fractions. Following are some of the outputs I saw,

-149399166081040384.00000000000000000000
128349565723082752.00000000000000000000
-323890424458510336.00000000000000000000
802221481969844224.00000000000000000000
817395979383734272.00000000000000000000

They are always like these, and it doesn't matter if I change the bounds. What is wrong here?




Aucun commentaire:

Enregistrer un commentaire