I'm used to typing random.randrange
. I'll do a from random import Random
to spot the error from now on.
For a game involving procedural generation (nope, not a Minecraft clone :p) I'd like to keep several distinct pseudo-random number generators:
The rationale being that I want to be able to reproduce the first, so I don't want the second one to interfere.
I thought random.Random
was made for that. However something is puzzling me:
import random
rnd = random.Random()
rnd.seed(0)
print [random.randrange(5) for i in range(10)]
rnd.seed(0)
print [random.randrange(5) for i in range(10)]
produces two different sequences. When I do rnd = random
then things work as expected, but I do need several generators.
What am I missing?
It works almost exactly as you tried but the rnd.seed() applies to the rnd object
just use
rnd = random.Random(0) # <<-- or set it here
rnd.seed(7)
print [rnd.randrange(5) for i in range(10)]
or by setting the global seed, like this:
random.seed(7)
print [random.randrange(5) for i in range(10)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With