For most people computers have always been these mysterious magical black boxes that just do strange magical things inside. Unsurprisingly, this has resulted in a lot of misconceptions about how they work and what they are doing.
Curiously, some misconceptions became prevalent even among more tech-savvy people who do know more about how computers actually work, and should know better. You often hear these misconceptions and false notions repeated over and over. Here are a few of them, related to how computer handle and calculated with numbers.
1: "Pseudorandom" = "bad randomness"
This one is so widespread and so commonly repeated that it's outright frustrating. People just love to repeat the "tech-savvy" fact that computers can't actually generate truly random numbers, and that when they generate "random" numbers, they are just following a completely deterministic algorithm that always produces the same string of numbers starting with a given seed. The fancy term for this is "pseudorandom", which quite literally actually means "not actually random", and could even be translated as "fake randomness".
That's actually not incorrect. It really is how it actually works. (Yes, there are some CPUs and chips that can generate actual random numbers by eg. sampling resistor noise and other similar methods, but we are not talking about those, but about how computers and computer programs normally produce "random" numbers.)
The problem is not that that would be incorrect. The problem is that a lot of people who are trying to lecture others about the true nature of computer-generated random numbers seem to think and, thus, heavily imply, or even directly say, that this means that the quality of those random numbers is very poor. That they are very bad random numbers. After all, they aren't truly random.
Indeed, the topic quite often comes up when comparing computer-generated random numbers to sources of true randomness, often with the heavy implication (or even outright statement) that the random numbers produced by the source of true randomness are somehow "better", "more random", and of "higher quality", compared to the bad randomness of the computer-generated ones.
There are, indeed, extremely poor random number generator algorithms, many of which are way, way too common in computer programs. But that's generally not what they are talking about. The implication is that computer-generated pseudorandom numbers are always of poor quality, "less random" and "bad", regardless of which algorithm is being used, by the very nature of it being deterministic rather than actually random.
This is a complete misconception.
In actual reality, if you take a good-quality random number generator, the "quality" of the randomness it produces, no matter what metric you may want to use, will be completely equal (and in some ways actually even better) than a source of pure randomness. This is especially the case with so-called cryptographically strong pseudorandom number generators (CSPRNG).
This means that if you take a string of numbers generated by a CSPRNG and another generated by a high-quality true source of randomness (assuming it has been perfectly calibrated to produce evenly distributed numbers), no matter how many numbers either produced, even if it's billions and billions of numbers, there is no way to distinguish which is which. There is no algorithm, no method, no test, no heuristic, that you could use to determine for certain which one is the CSPRNG and which one is the true source of randomness.
In fact, perhaps somewhat ironically, if the true source of randomness is not perfectly calibrated, you actually can make the distinction... because the true source of randomness will be worse! In other words, true sources of randomness often exhibit some kind of bias because they often are not generating random numbers with a completely even probability. Ironically, in order to try to compensate for these biases, true sources of randomness are often run through hashing algorithms that are extremely similar or almost identical to the algorithms used by CSPRNGs. Without such hashing you often get uneven randomness (which is often detrimental to tasks that require even randomness.)
So, somewhat ironically, CSPRNGs are, in a sense, even better than "true" sources of randomness. Their primary difference is that the true sources are not repeatable, in other words, you can't make them produce the same stream of numbers multiple times (a feature that's actually a lot more useful than many people realize.)
The fact that PRNGs produce the same stream of numbers when starting from the same initial seed does not make the quality of their randomness any worse, by any metric that you may use. As mentioned, with CSPRNGs, if you are not told the initial seed, you literally have no way of knowing if it's "true" randomness or not. (In fact, the cryptographic strength of those PRNGs relies on that fact!)
2: Floating point numbers have indeterminate values
Most programmers know that when using floating point numbers, you cannot rely on the results of calculations being exact. For example 0.1+0.1 might not result in exactly 0.2 but a value extremely close to it. Comparing the result of a calculation to an exact value might yield them incorrectly being compared as unequal even though mathematically the calculation should have produced that value.
This fact has caused some programmers and other people to form the misconception that floating point values are somehow "fuzzy" and "indeterminate", as if they had some uncertainty to them. As if taking the same floating point value and comparing it to another multiple times could give different results at different times. You can't rely on them being accurate because they "hover" around the actual true value, like they were some kind of probabilistic values governed by the Heisenberg Uncertainty Principle. They might even think that the exact same calculation, exact same code, run multiple times, may produce slightly differing values. In other words, if you call a function that returns a floating point value, and then call it again with the exact same parameters, and then compare the two return values, they might compare unequal!
They might not completely explicitly state it like that, but the way they talk and describe floating point numbers, it certainly makes it sound like they think like that. Or, at the very minimum, don't know for certain and have a vague fuzzy conception about it (a bit like the floating point numbers themselves!)
The actual reality is much more mundane: It's just rounding errors. That's it. There's nothing more to it.
And yes, the exact same code run twice will always produce the bit-by-bit exact same result, every time! (In fact, computers rely on that being the case. If they didn't, something would be seriously wrong and the CPU would malfunction.)
Floating point values are no different from integers: They have a limited number of bits to represent numbers, and not all numbers are representable with a limited number of bits. The main difference between integers types and floating point types is that the latter drop extra bits from the low end (while integer types drop them from the high end.) That's a quite technical way of saying that floating point types round to the nearest representable value when it cannot be represented exactly.
To understand this better, suppose that we are using base-10 decimal numbers, and we can only store 3 digits. In other words, we can represent accurately values like 125, 23.4, 3.45, and 0.456 (leading zeroes do not matter in this storage so they don't count towards the number of representable digits.)
But what happens if some calculation would give us, for example, 1.234? We can't store that in our three-digit system! Well, we have no choice but to drop that last 4, rounding the value to the closest representable value, which in this case would be 1.23. If the calculation would give us 1.577 instead, we round it to 1.58. And so on.
This is essentially what's happening with floating point numbers, just in base-2 instead of base-10. It's just rounding to the nearest representable value. There's nothing more to it.
One of the problems that commonly happens is, of course, that rounding errors accumulate. Do a hundred such calculations, using the result of the previous calculation at each step, and the rounding errors can cause the end result to deviate by quite a bit. It might still be just a deviation in the 14th decimal place, but it's still different from what it mathematically should have been. Rounding to the nearest tries to minimize these cumulative rounding errors, but they are inevitable.


_poster.jpg)