[Python-ideas] Python's Source of Randomness and the random.py module Redux

Andrew Barnert abarnert at yahoo.com
Fri Sep 11 05:18:45 CEST 2015

On Sep 10, 2015, at 19:48, Nick Coghlan <ncoghlan at gmail.com> wrote:
> On 11 September 2015 at 08:54, Andrew Barnert via Python-ideas
> <python-ideas at python.org> wrote:
>> Actually, I just thought of an even simpler solution:
>> Add a deterministic_singleton member to random (which is just initialized to DeterministicRandom() at startup). Now, the user fix is just to change "import random" to "from random import deterministic_singleton as random".
> Change the spelling to "import random.seeded_random as random" and the
> user fix is even shorter.

OK, sure; I don't care much about the spelling. I think neither name will be unduly confusing to novices, and anyone who actually wants to understand what the choice means will use help or the docs or a Google search and find out in a few seconds.

> I do agree with the idea of continuing to provide a process global
> instance of the current PRNG for ease of migration - changing a single
> import is a good way to be able to address a deprecation, and looking
> for the use of seeded_random in a security sensitive context would
> still be fairly straightforward.

Personally, I think we're done with that change.  Deprecation of the names random.Random, random.random(), etc. is sufficient to prevent people from making mistakes without realizing it. Having a good workaround to prevent code churn for the thousands of affected apps means the cost doesn't outweigh the benefits. So, the problem Theo raised is solved.[1] Which means the more radical solution he offered is unnecessary. Unless we're seriously worried that some people who aren't sure if they need Seeded or System may incorrectly choose Seeded just because of performance, there's no need to add a Chacha choice alongside them. Put it on PyPI, maybe with a link from the SystemRandom docs, and see how things go from there.

[1] Well, it's not quite solved, because someone has to figure out how to organize things in the docs, which obviously need to change. Do we tell people how to choose between creating a SeededRandom or SystemRandom instance, then describe their interface, and then include a brief note "... but for porting old code, or when you explicitly need a globally shared Seeded instance, use seeded_random"? Or do we present all three as equally valid choices, and try to explain why you might want the singleton seeded_random vs. constructing and managing an instance or instances?

More information about the Python-ideas mailing list