This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients vstinner
Date 2020-04-14.22:30:17
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1586903418.02.0.530735802731.issue40286@roundup.psfhosted.org>
In-reply-to
Content
The random module lacks a getrandbytes() method which leads developers to be creative how to generate bytes:
https://stackoverflow.com/questions/5495492/random-byte-string-in-python

It's a common use request:

* bpo-13396 in 2011
* bpo-27096 in 2016
* https://bugs.python.org/issue40282#msg366444 in 2020

Python already has three functions to generate random bytes:

* os.getrandom(): specific to Linux, not portable
* os.urandom()
* secrets.token_bytes()

These 3 functions are based on system entropy and they block on Linux until the kernel collected enough entropy: PEP 524.

While many users are fine with these functions, there are also use cases for simulation where the security doesn't matter, and it's more about being able to get reproducible experience from a seed. That's what random.Random is about.

The numpy module provides numpy.random.bytes(length) function for such use case:
https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.random.bytes.html

One example can be to generate UUID4 with the ability to reproduce the random UUID from a seed for testing purpose, or to get reproducible behavior.

Attached PR implements the getrandbytes() method.
History
Date User Action Args
2020-04-14 22:30:18vstinnersetrecipients: + vstinner
2020-04-14 22:30:18vstinnersetmessageid: <1586903418.02.0.530735802731.issue40286@roundup.psfhosted.org>
2020-04-14 22:30:18vstinnerlinkissue40286 messages
2020-04-14 22:30:17vstinnercreate