[Python-ideas] Python's Source of Randomness and the random.py module Redux
abarnert at yahoo.com
Fri Sep 11 22:27:02 CEST 2015
On Sep 11, 2015, at 06:49, Steven D'Aprano <steve at pearwood.info> wrote:
>> On Thu, Sep 10, 2015 at 04:08:09PM +1000, Chris Angelico wrote:
>> On Thu, Sep 10, 2015 at 11:50 AM, Andrew Barnert via Python-ideas
>> <python-ideas at python.org> wrote:
>>> Of course it adds the cost of making the module slower, and also
>>> more complex. Maybe a better solution would be to add a
>>> random.set_default_instance function that replaced all of the
>>> top-level functions with bound methods of the instance (just like
>>> what's already done at startup in random.py)? That's simple, and
>>> doesn't slow down anything, and it seems like it makes it more clear
>>> what you're doing than setting random.inst.
>> +1. A single function call that replaces all the methods adds a
>> minuscule constant to code size, run time, etc, and it's no less
>> readable than assignment to a module attribute.
> Making monkey-patching the official, recommended way to choose a PRNG is
> a risky solution, to put it mildly.
But that's not the proposal. The proposal is to make explicitly passing around an instance the official, recommended way to choose a PRNG; monkey-patching is only the official, recommended way to quickly get legacy code working: once you see the warning about the potential problem and decide that the problem doesn't affect you, you write one standard line of code at the top of your main script instead of rewriting all of your modules and patching or updating every third-party module you use.
As I said later, I think my later suggestion of just having a singleton DeterministicRandom instance (or even a submodule with the same interface) that you can explicitly import in place or random serves the same needs well enough, and is even simpler, and is more flexible (in particular, it can also be used for novices' "my first game" programs), so I'm no longer suggesting this. But that doesn't mean there's any benefit to mischaracterizing the suggestion (especially if Chris or anyone else still supports it even though I don't).
More information about the Python-ideas