[Python-Dev] Mersenne Twister

Guido van Rossum guido@python.org
Thu, 29 Aug 2002 15:29:21 -0400


> > -- Add a version number argument to Random() which defaults to two.
> 
> Why not have a WHRandom() and a MersenneRandom() instance inside module
> random?  That way you can even give a future behavior warning that
> Random() is about to change and people can either choose the particular
> generator they want or accept the default.
> 
> To my mind, this is a case of explicit (actually naming the generator
> types) is better than implicit (version number?  Where's my documentation?
> Which generator is which version?)  Maybe this isn't a big deal now, but I
> can believe that we might accumulate another RNG or two (there are some
> good reasons to want *weaker* or correlated RNGs) and having a weaker
> generator with a *later* version number is just bound to cause havoc.

Hm, I hadn't realized that the random.Random class doesn't import the
whrandom module but simply reimplements it.

Here's an idea.

class BaseRandom: implements the end user methods: randrange(),
choice(), normalvariate(), etc., except random(), which is an abstract
method, raising NotImplementedError.

class WHRandom and class MersenneRandom: add the specific random
number generator implementation, as random().

Random is an alias for the random generator class of the day,
currently MersenneRandom.

Details: can MersenneRandom support jumpahead()?  Should it support
whseed(), which is provided only for backwards compatibility?

If someone pickles a Random instance with Python 2.2 and tries to
unpickle it with Python 2.3, this will fail, because (presumably) the
state for MersenneRandom is different from the state for WHRandom.
Perhaps there should be a module-level call to make Random an alias
for WHRandom rather than for MersenneRandom.

--Guido van Rossum (home page: http://www.python.org/~guido/)