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
Comments
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') |
How random would be the 0 returned by getrandbits(0)? :-) |
I think you know the answer to your question ;-) |
Seconded. And I wish to add the getrandbytes() method. |
This was discussed previously in bpo-37000. I agree that |
+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. |
I do not want to open an issue if I know that the idea will be rejected. |
About a hypothetical getrandbytes(), probably 90% of my uses of getrandbits() have been to generate random bytestrings. |
Why not use secrets.token_bytes() or randrange(2**b).to_bytes()? Do you really need an API extension? |
I agree I don't need it per se. However, I suspect that for non-exports it would be easier than As for |
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: 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". |
I created bpo-40286: Add getrandbytes() method to random.Random. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: