This has been making my head hurt. I have also seen a detailed post on stack overflow about this situation that is a few years old and I would like to highlight some of the flaws from the discussions. I will highlight them in the next section. But the problem is outlined below.
I am trying to find out if the random.uniform(0,1) function from the random library,
import random
can indeed output a value of 0 given a range greater than 0 excluding special case random.uniform(0,0) (range here is 0).
Special Case
>>>random.uniform(0,0)
0.0
Test
>>>random.uniform(0,1)
0.11689643963821128
Upon doing some research about IEEE 754 standard for double precision floating point numbers which are used in python for this function, I have not been able to prove that 0 cannot be a result of random.uniform(x,y) where (x!=y and x,y !=0)See expectation.
But this leads me to another question why does IEEE754 standard round to 0 if and only if it is 0, but not any other number unless proving otherwise obviously.
I want to say it's impossible, but wondering what you guys think.
I have used this website to check for a few numbers too but with no luck.
I have tried using various loops some even give me outputs closer to 1 when using this function, I need to do some more testing before backing this up, but using values like that on the website provided above, provide weird values.
The link to the old stackoverflow thread can be found here
One of the responses suggest that
>>> random.uniform(0., 5e-324)
0.0
will indeed give you a value of 0 however it can also give you this (is it a 50% chance?)
>>> random.uniform(0., 5e-324)
5e-324
other times it has shown to me to be infinite in VS Code Debugger when used in a loop until reaching 0.0 for some reason upon a random interval.
So this also begs the question does python really process 324 digits of precision using the uniform specification without needing to specify that we really want a range between 0 and 5e-324. With an integer range it only produces about 16 digits of precision.
There are some other points that I have read through on the older thread, but I am struggling to relate here to it.
So can random.uniform(0,1) actually produce 0.0? If you have tried experimenting or researching this in other languages, please share your thoughts and results.
(I may be making more edits to this question to make implement further testing or clearing and further explaining to some points made)