-2

I need to get a random value, after lots of operations. I see, that if I write, e.g. 1000000 and divide it to 10 for 100 times, I should get an almost random number.

double nump = 1000000000;
cout.precision(45);
for (int i = 1; i <= 100; i++) {
    nump = nump / 10;
}
cout << nump;

But if I launch this code, I get every time the similar numbers. Where is inaccuracy which have machines? Why is this so accuracy? How to make such a calculation, that will lead to big inaccuracy?

  • random numbers have some very well defined properties, eg the follow a certain distribution, and there are important measures for quality of randomness. Even if you would get numbers that appear to be random in this way, they are practically useless as pseudo random numbers – 463035818_is_not_an_ai Sep 04 '19 at 11:30
  • 4
    How could dividing numbers possibly get you a random number? – vgru Sep 04 '19 at 11:30
  • 3
    More important: how do you expect this code to give different result each time you launch it? Your code is fully deterministic. I think you confuse accuracy with randomness. These are completely different things. – freakish Sep 04 '19 at 11:31
  • 2
    So this would be something between a duplicate of [How to get a random number in C++](https://stackoverflow.com/q/13445688/69809) and and [Is floating point math broken](https://stackoverflow.com/q/588004/69809). – vgru Sep 04 '19 at 11:34
  • 2
    If your system uses [IEEE754](https://en.wikipedia.org/wiki/IEEE_754) (very likely) then double-precision floating point numbers only have a little under 16 decimal digits of precision. Anything more than that won't give accurate results. – Some programmer dude Sep 04 '19 at 11:34
  • It seems like you are looking for math correct pseoudo-random number generator. Check [List of random number generators](https://en.wikipedia.org/wiki/List_of_random_number_generators). And check for the [PCG](https://en.wikipedia.org/wiki/Permuted_congruential_generator) – Victor Gubin Sep 04 '19 at 11:40

2 Answers2

8

You are misunderstanding what floating point accuracy means. It means that the value stored in a floating point variable (float or double) is not necessary exactly the same value as mathematically assigned/computed. This happens because not all mathematical real numbers can be represented in a floating point type.

It does not mean you will get different results when you perform the same instructions on the same values on the same build on the same machine.

For instance the integer value 16,777,217 cannot be represented in IEE 754 float. So in

float a = 16'777'217;

a will be "inaccurate" in the sense that it is not 16'777'217, but it will always have the same "inaccuracy".


I need to get a random value

Then use the C++11 random library.

bolov
  • 72,283
  • 15
  • 145
  • 224
2

There is no inaccuracy and therefore no randomness.

Just like the integers are an approximation for real numbers, the same can be said for floating point numbers. The distribution of floating point numbers across the reals is even in logarithmic space in the same way that the distribution of integers is even in real space.

When you write a number like 1.234 and assign that to a double, the closest double to 1.234 is picked. This is different to assigning that to an int where the nearest int towards 0 is picked. But the principle is the same.

When you compute nump / 10;, floating point standards (e.g. IEEE754) often require that the closest double to the result is picked.

If you need pseudo-random numbers then use appropriate functions that are part of the C++ standard library.

If you want true random numbers, and are very rich, then you can acquire some hardware for generating them, else you can use someone else's hardware: e.g. https://qrng.anu.edu.au/

Bathsheba
  • 231,907
  • 34
  • 361
  • 483