[issue40286] Add getrandbytes() method to random.Random

2020-04-14 Thread STINNER Victor


New submission from STINNER Victor :

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.

--
components: Library (Lib)
messages: 366454
nosy: vstinner
priority: normal
severity: normal
status: open
title: Add getrandbytes() method to random.Random
versions: Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40286] Add getrandbytes() method to random.Random

2020-04-14 Thread STINNER Victor


Change by STINNER Victor :


--
keywords: +patch
pull_requests: +18877
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/19527

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40286] Add getrandbytes() method to random.Random

2020-04-14 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

If we have to have this, the method name should be differentiated from 
getrandbits() because the latter returns an integer.  I suggest just 
random.bytes(n), the same as numpy.

> Python already has three functions to generate random bytes:

Now, there will be four ;-)

--
nosy: +rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40286] Add getrandbytes() method to random.Random

2020-04-15 Thread Antoine Pitrou


Change by Antoine Pitrou :


--
nosy: +mark.dickinson

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40286] Add getrandbytes() method to random.Random

2020-04-15 Thread Vedran Čačić

Vedran Čačić  added the comment:

> I suggest just random.bytes(n), the same as numpy.

The problem with this is that people who `from random import *` (some schools 
insist on this, probably because most functions they need already start with 
`rand`) will shadow builtin `bytes`. Not that those schools do anything with 
`bytes`, but still, it might be inconvenient.

(The metaproblem is of course that some functions already do the "poor man's 
namespacing" in C-style by starting with `rand`, and some don't. I'm always for 
user control of namespacing, but I'm just saying that it doesn't correspond to 
how many beginners use `random` module.)

--
nosy: +veky

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40286] Add getrandbytes() method to random.Random

2020-04-15 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

Do you have another name suggestion that doesn't have a parallelism problem 
with the existing name?   The names getrandbytes() and getrandbits() suggest a 
parallelism that is incorrect.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40286] Add getrandbytes() method to random.Random

2020-04-15 Thread Vedran Čačić

Vedran Čačić  added the comment:

I think that the "module owner";-P must decide whether the `random` module 
should follow the C-namespacing or not. Of course, I'm in the "not" camp, so I 
believe those two "rand..." functions (randrange is completely redundant with 
random.choice(range)) should be supplemented with random.int and random.float. 
And then random.bytes will be completely natural. And people might be gently 
nudged into the right direction when using Python module namespaces.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40286] Add getrandbytes() method to random.Random

2020-04-15 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

I concur that bytes() isn't a good name, but am still concerned that the 
proposed name is a bad API decision.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40286] Add getrandbytes() method to random.Random

2020-04-15 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

Maybe randbytes()?

--
nosy: +serhiy.storchaka

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40286] Add getrandbytes() method to random.Random

2020-04-15 Thread STINNER Victor


STINNER Victor  added the comment:

I like "from random import randbytes" name. I concur that "from random import 
bytes" overrides bytes() builtin type and so can likely cause troubles.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com