Skip to content

random.Random._randbelow_with_getrandbits loops forever for n=0 #153367

Description

@Dutcho

Bug report

Bug description:

random.Random()._randbelow_with_getrandbits(0)  # loops indefinitely

Same applies to (conditional) synonym ._randbelow().

Note the alternative implementation (below) fails:

random.Random()._randbelow_without_getrandbits(0)  # raises ZeroDivisionError

I suggest adding a check on lower bound (like _randbelow_without_getrandbits has on upper bound).

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedstdlibStandard Library Python modules in the Lib/ directorytype-refactorCode refactoring (with no changes in behavior)

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions