Reputation: 5743
I have an understanding (on a very high level) about the usage of seed to generate the random numbers. so setting a particular seed prior to generating random numbers would result in generating the same numbers at each call.
I am assuming restoring the state by
import random
test123 = random.getstate()
random.setstate(test123)
would also result in the replication on random numbers by some similar process
i am looking for an understanding in their similarities and differences. For example: are setting the seed to some value and restoring the internal state of the generator via random.setstate(random.getstate()) certain methods in every situation for replicating random numbers?
there is not much documented about them that i could find
Upvotes: 4
Views: 357
Reputation: 522016
The seed is a simple initial value that you can pass from which the generator will be initialised. The state on the other hand is the full current internal state of the generator. Yes, setting a seed and setting a state are fundamentally the same thing, in that it allows you to replay a specific sequence of pseudo-random numbers. However, internally the generator doesn't work with simple values like a seed. What the get
/setstate
methods allow you to do is to programmatically restore a state from a running generator; while a seed value is rather something you'd supply as an argument from a configuration file for example.
Let's talk about use cases: you have some algorithm which involves a random value, and you want to test that algorithm. For it to be testable, it must be reproducible. That means you need to be able to control the random element within your algorithm. That's where PRNGs come in, they're predictable and repeatable (unlike true RNGs). You would write some test cases which specify a simple seed to set the PRNG into a specific state. Seeds are short readable values you can easily write into test cases.
Now, if you want to interrupt your test case at some point to inspect it, and then perhaps take a step back and rerun one specific step (whether manually or programmatically), you'll want to get the specific state of the PRNG to be able to reset it to that specific state later. Now, there's no getseed
method on the PRNG because it doesn't internally work with values like the seed you first supplied; but it has a getstate
method which serves the same purpose, its return value is just more complex.
Upvotes: 7