Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

random.getrandbits(0) should succeed #84463

Closed
pitrou opened this issue Apr 14, 2020 · 13 comments
Closed

random.getrandbits(0) should succeed #84463

pitrou opened this issue Apr 14, 2020 · 13 comments
Labels
3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@pitrou
Copy link
Member

pitrou commented Apr 14, 2020

BPO 40282
Nosy @tim-one, @rhettinger, @mdickinson, @pitrou, @vstinner, @stevendaprano, @serhiy-storchaka
PRs
  • bpo-40282: Allow random.getrandbits(0) #19539
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-04-17.17:32:30.364>
    created_at = <Date 2020-04-14.15:32:35.314>
    labels = ['type-feature', 'library', '3.9']
    title = 'random.getrandbits(0) should succeed'
    updated_at = <Date 2020-04-17.17:32:30.363>
    user = 'https://github.com/pitrou'

    bugs.python.org fields:

    activity = <Date 2020-04-17.17:32:30.363>
    actor = 'pitrou'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-04-17.17:32:30.364>
    closer = 'pitrou'
    components = ['Library (Lib)']
    creation = <Date 2020-04-14.15:32:35.314>
    creator = 'pitrou'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40282
    keywords = ['patch']
    message_count = 13.0
    messages = ['366392', '366393', '366394', '366429', '366430', '366440', '366443', '366444', '366445', '366446', '366452', '366455', '366670']
    nosy_count = 7.0
    nosy_names = ['tim.peters', 'rhettinger', 'mark.dickinson', 'pitrou', 'vstinner', 'steven.daprano', 'serhiy.storchaka']
    pr_nums = ['19539']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue40282'
    versions = ['Python 3.9']

    @pitrou
    Copy link
    Member Author

    pitrou commented Apr 14, 2020

    When creating variable-sized random binary strings with random.getrandbits(), you currently have to special case when the number of bytes is 0, because otherwise getrandbits() raises:

    ValueError: number of bits must be greater than zero

    It seems like it wouldn't hurt to simply return 0 in that case.

    The actual snippet looks something like:

    random.getrandombits(nbytes * 8).to_bytes(nbytes, 'little')

    @pitrou pitrou added 3.9 only security fixes type-feature A feature request or enhancement labels Apr 14, 2020
    @vstinner
    Copy link
    Member

    How random would be the 0 returned by getrandbits(0)? :-)

    @pitrou
    Copy link
    Member Author

    pitrou commented Apr 14, 2020

    I think you know the answer to your question ;-)

    @serhiy-storchaka
    Copy link
    Member

    Seconded.

    And I wish to add the getrandbytes() method.

    @mdickinson
    Copy link
    Member

    This was discussed previously in bpo-37000.

    I agree that getrandbits(0) should succeed.

    @rhettinger
    Copy link
    Contributor

    +0 for having getrandbits(0) return 0. Conceptually, it is reasonable. Practically, it is a bit inconvenient because the ValueError may have to be moved upstream to the _randbelow() methods.

    -1 for getrandbytes(). That is feature creep and no user has requested it. Also, the name leads to a confusing API with getrandbits() returning arbitrary sized python ints and getrandbytes() returning bytes. Lastly, it mostly duplicates functionality already supplied by secrets.token_bytes(). If you really want this, open another tracker issue and don't derail the issue at hand.

    @serhiy-storchaka
    Copy link
    Member

    I do not want to open an issue if I know that the idea will be rejected.

    @pitrou
    Copy link
    Member Author

    pitrou commented Apr 14, 2020

    About a hypothetical getrandbytes(), probably 90% of my uses of getrandbits() have been to generate random bytestrings.

    @pitrou pitrou added the stdlib Python modules in the Lib dir label Apr 14, 2020
    @rhettinger
    Copy link
    Contributor

    Why not use secrets.token_bytes() or randrange(2**b).to_bytes()? Do you really need an API extension?

    @pitrou
    Copy link
    Member Author

    pitrou commented Apr 14, 2020

    I agree I don't need it per se. However, I suspect that for non-exports it would be easier than
    getrandbits(nbytes * 8).to_bytes(nbytes, 'endian').

    As for secrets.token_bytes(), it's not really adequate for regular pseudo-random data generation when you want to use a fixed seed. And I'm not sure what its performance characteristics are when you pass a large size.

    @vstinner
    Copy link
    Member

    That is feature creep and no user has requested it.

    Python already provides such function in the secrets module, so I'm not sure if what you mean that "no users has requested it". secrets.token_bytes() exists because there is a need for such function.

    secrets.token_bytes() is more designed for security, but random.Random() is more designed for simulations. And such users also exists, that's why numpy provides numpy.random.bytes(length) function:
    https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.random.bytes.html

    To be honest, I never understood where there is such "hole" in the random module API. Especially for SystemRandom since its source os.urandom() generates bytes.

    A concrete use case is to generate manually a UUID4 from 16 random bytes. For testing, you may want to get "deterministic random" UUID4. Using getrandbits() for thta sounds unnatural to me.

    Another use case is to create a secret token: well, that's basically that secrets.token_bytes() does. That's used in multiprocessing but also in urllib (AbstractDigestAuthHandler.get_cnonce()).

    So yeah, it sounds perfectly reasonable to add such simple function. I don't see how add such obvious function would be a "feature creep".

    @vstinner
    Copy link
    Member

    I created bpo-40286: Add getrandbytes() method to random.Random.

    @pitrou
    Copy link
    Member Author

    pitrou commented Apr 17, 2020

    New changeset 75a3378 by Antoine Pitrou in branch 'master':
    bpo-40282: Allow random.getrandbits(0) (GH-19539)
    75a3378

    @pitrou pitrou closed this as completed Apr 17, 2020
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants