foxpy / qc

Quad C: Custom Cruel C Crate
MIT License
4 stars 0 forks source link

floating point random generator outputs just a half of the numbers it could possibly generate #46

Closed foxpy closed 3 years ago

foxpy commented 3 years ago

Taken from this: http://prng.di.unimi.it/ Probably, a multiplication approach is the best one, and it is really-really fast on modern hardware, which tends to be specificly designed to chew as much FLOPs per second as possible.

peteroupc commented 3 years ago

Responding to the title:

Although that may be true, even multiplying a random integer by a constant will not necessarily produce all the numbers in [0, 1] that a floating-point format may represent (this is theoretically the case for binary64 if the integer is 1076 bits long or longer, but there are subtle issues like rounding and this particular bit length can't be universally applied for all floating-point formats). F. Goualard has surveyed approaches of dividing random integers this way in popular programming environments.

In addition, there are procedures for giving each floating-point number the expected probability of occurring, while taking care of rounding issues, among other things, and examples of this are the so-called "Rademacher Floating-Point Library" by C. Conrads, as well as an algorithm I have described. However, as you can see, these procedures are far from trivial.

foxpy commented 3 years ago

Although that may be true, even multiplying a random integer by a constant will not necessarily produce all the numbers in [0, 1]

By my naive reasonings, multiplication approach is going to produce more numbers in [0, 1] space than copying bits directly into mantissa and subtracting literal 1.0. It is also mentioned on the website from my first comment that this approach is more preferable.

However, as you can see, these procedures are far from trivial.

Procedure you can see in qc I have written completely by myself just by reading Wikipedia article about double precision floating points. It is as close to trivial as I like it. And it seems to produce very good numbers.

Thank you for your post, now I am studying the papers you have pointed me to. What I would love to see in qc is a trivial approach which produces best results out of all trivial approaches.

foxpy commented 3 years ago

Turns out, among other popular solutions, the solution I have introduced by myself is not even the worst one. I should probably not change behaviour of floating point random generator in qc as it may be insufficient only for some serious simulations or scientific studies.