-
-
Notifications
You must be signed in to change notification settings - Fork 33.6k
Closed as not planned
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
random.uniform can overflow to inf outside the range of [a, b] in some cases:
import random
random.uniform(-1e308, 1e308) # math.infThe implementation of random.uniform is:
def uniform(self, a, b):
return a + (b - a) * self.random()where (b - a) can overflow to math.inf: assert 1e308 * 2 == math.inf.
Since the docstring of random.uniform guarantees that the return value will be in the range [a, b], I would classify this as a bug.
I see a few solutions:
- Error on overflow.
- numpy does this:
numpy.random.uniform(-1e308, 1e308)raisesOverflowError.
- numpy does this:
- Document that
random.uniformmay overflow tomath.inf, even ifb < math.inf. - Clamp out-of-bounds values (only if
b - a == math.inf, for performance).
CPython versions tested on:
3.12
Operating systems tested on:
macOS
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error