On Sat, Mar 07, 2015 at 09:06:13PM +0100, Floris van Manen wrote:
I'm using the jumpahead() to have multiple streams in parallel, derived from a single point.
I'm afraid I don't understand what that sentence means. Do you mean derived from a single seed? If so, I think the easiest way is to just create multiple random instances. Suppose you want five streams of random numbers: create one extra "seeder" (or just use the pre-defined top-level random functions): import random seeder = random.Random(myseed) # or just use random.seed(myseed) streams = [random.Random(seeder.random()) for _ in range(5)] If you want to reset them to the initial state, just reseed the seeder stream and recreate them. Or you can just grab all their internal states: states = [s.getstate() for s in streams]
def jumpRandomState(self): self.random.setstate(self.randomState) self.random.jumpahead(1) self.randomState = self.random.getstate()
I'm afraid I don't understand the purpose of that method or why you are restoring the internal state before jumping. I tried putting it in a subclass of random.Random, but it raises an exception: py> class MyRandom(random.Random): ... def jumpRandomState(self): ... self.random.setstate(self.randomState) ... self.random.jumpahead(1) ... self.randomState = self.random.getstate() ... py> rnd = MyRandom(1000) py> rnd.jumpRandomState() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in jumpRandomState AttributeError: 'builtin_function_or_method' object has no attribute 'setstate' so your code doesn't work for me. Based just on the name, I would do this: class MyRandom(random.Random): def jumpRandomState(self): self.jumpahead(356789) If you don't trust jumpahead to reliably jump to an independent part of the PRNG sequence, re-seed with an independent pseudo-random number: class MyRandom(random.Random): _JUMPER = random.Random() def jumpRandomState(self): self.seed(self._JUMPER.random()) -- Steve