Re: creating an instance of a random number generator

Guido.van.Rossum@cwi.nl
Fri, 07 Apr 1995 10:34:11 +0200

> > I am thinking that saying
> > import whrandom
> > wh = whrandom(seed1, seed2, seed3)
> > should give me an instance of a random-number generator; I could then
> > use it by saying
> > rand = wh.random()
> >
> > Python disagrees; it claims that "wh = whrandom(seed1, seed2, seed3)"
> > is a "call of a non-function".
>
> whrandom is a _module_, not a class. What you want is:
>
> import whrandom
> whrandom.seed(seed1, seed2, seed3)
> rand = whrandom.random()
>
> Unfortunately, this does not help your thread problem. AFAIK, there is no
> way to have multiple random number classes, each with their own set of
> seeds, etc.

Actually, *module* whrandom also defines a *class* whrandom. The name
of this class is "whrandom.whrandom". So you can create a new random
generator as follows:

import whrandom
wh = whrandom.whrandom(seed1, seed2, seed2)

Note that the import statement should not be done in each thread but
at the module level -- when two threads independently but
simultaneously try to import a module, things get messed up.

--Guido van Rossum, CWI, Amsterdam <mailto:guido@cwi.nl>
<http://www.cwi.nl/~guido/>