[Python-ideas] Globally configurable random number generation
cody.piersall at gmail.com
Mon Sep 14 18:28:49 CEST 2015
On Mon, Sep 14, 2015 at 8:32 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> This is an expansion of the random module enhancement idea I
> previously posted to Donald's thread:
> I'll write it up as a full PEP later, but I think it's just as useful
> in this form for now.
> * expose a global SystemRandom instance as random.system_random
> * provide a random.set_default_instance() API that makes it possible
> to specify the instance used by the module level methods
> * the module level seed(), getstate(), and setstate() functions will
> throw RuntimeError if the corresponding method is missing from the
> default instance
One problem that people (I can't remember who) have pointed out about
random.set_default_instance() is that any imported module in the same
process can change the random from secure -> insecure at a distance.
One way to solve this is to ensure that set_default_instance() can be
called only once; if it is called more than once, a RuntimeError could
be raised. I think the logging module does something like this for
setting the logging level?
I think the only way that this really would make sense would be to make
set_default_instance() be called before any of the module level functions.
The first time a module level function is called, you could default to
selecting the CSRNG. If you call one of the seeded API functions
(getstate, setstate, seed) before the other module-level functions the
instance could default to the deterministic RNG, but that might be
confusing to debug. I could imagine people getting really confused
if this program worked:
but this program failed:
random.seed(1234) # would raise a RuntimeError
random.random() # would not be reached
I'm not crazy about the idea of changing the default instance based on the
first module level function called; that might be a terrible idea. But I
_do_ think it's a good idea not to let the default instance change
throughout the life of the program.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas