[Tutor] multithreading random()

Dick Moores rdm at rcblue.com
Tue Oct 10 10:03:30 CEST 2006


At 09:57 PM 10/9/2006, you wrote:
>On 10/10/06, Dick Moores <rdm at rcblue.com> wrote:
> > Please refer to
> > <http://www.python.org/doc/current/lib/module-random.html>, from 
> which I quote:
> >
> > "The functions supplied by this module are actually bound methods of
> > a hidden instance of the random.Random class. You can instantiate
> > your own instances of Random to get generators that don't share
> > state. This is especially useful for multi-threaded programs,
> > creating a different instance of Random for each thread, and using
> > the jumpahead() method to make it likely that the generated sequences
> > seen by each thread don't overlap."
> >
> > Could someone point me to an example script where this was done? I'd
> > very much like to learn how to do that.
>
>Using google's code search, I found this:
>
>     def _create_random_generators(self, num, delta, firstseed=None):
>         """Return list of 'num' distinct generators.
>         Each generator has its own unique segment of delta elements
>         from Random.random()'s full period.
>         Seed the first generator with optional arg firstseed (default
>         is None, to seed from current time).
>         """
>         g = Random(firstseed)
>         result = [g]
>         for i in range(num - 1):
>             laststate = g.getstate()
>             g = Random()
>             g.setstate(laststate)
>             g.jumpahead(delta)
>             result.append(g)
>         return result
>
>Although, from looking at the docs you quoted, this may not be correct
>for python2.3+.

Thanks, John, That's a start.

Didn't know about Google's code search, 
<http://www.google.com/codesearch>. What was your search string?

Dick







More information about the Tutor mailing list