Search code examples
pythonrandomdivide-by-zero

Is there a Python random.random() analog that does not generate 0


I'm writing some functions that require division by a random float, and while it's highly improbable that it will throw a ZeroDivisionError, I can't sleep until I'm positive it won't. Is there a random.random() analog for Python that's 0 exclusive.


Solution

  • As the random.random returns values [0.0, 1.0) as pointed out by Kevin Christopher Henry, it is easy to transfrom that into (0.0, 1.0] without any conditional code by:

    1.0 - random.random()