Python random.getstate() Function
The random.getstate() function in Python is used to retrieve an object capturing the current internal state of the random number generator. This object can later be passed to the setstate() method to restore the generator to this state. This function is part of the random module, which provides various functions to generate random numbers and sequences.
The primary purpose of this function is to capture the state of the generator at a specific moment, and later restore this state to effectively reproduce the same random values.
This function is not accessible directly, so we need to import the random module and then we need to call this function using random static object.
Syntax
Following is the syntax of the Python random.getstate() function −
random.getstate()
Parameters
This function does not accept any parameters.
Return Value
The random.getstate() function returns an object containing the current internal state of the generator.
Example
Let's look at an example of how to use the random.getstate() function to capture and restore the state of the random number generator.
import random # Initialize the random number generator random.seed(42) # Generate a sample of 10 numbers from a range of 20 print(random.sample(range(30), k=10)) # Capture the current state state = random.getstate() # Generate a sample of 20 numbers from a range of 20 print(random.sample(range(20), k=20)) # Restore the state random.setstate(state) # Generate another sample of 10 numbers from the same range print(random.sample(range(20), k=10))
When we run the above program, it produces the following result −
[20, 3, 0, 23, 8, 7, 24, 4, 28, 17] [2, 18, 13, 1, 0, 16, 3, 17, 8, 9, 15, 11, 12, 5, 6, 4, 7, 10, 14, 19] [2, 18, 13, 1, 0, 16, 3, 17, 8, 9]
Example
In this example, we'll demonstrate how to use the random.getstate() function to capture the state of the random number generator and then restore it to generate the same sequence of random numbers.
import random # Initialize the random number generator and get state random.seed(0) initial_state = random.getstate() # Generate and print random number print(random.random()) print(random.random()) # Setting the seed back to 0 resets the RNG back to the original state random.seed(0) new_state = random.getstate() assert new_state == initial_state # Since the state of the generator is the same as before, it will produce the same sequence print(random.random()) # We could also achieve the same outcome by resetting the state explicitly random.setstate(initial_state) print(random.random())
The output of the above code is as follows −
0.8444218515250481 0.7579544029403025 0.8444218515250481 0.8444218515250481
Example
Here is another example that compare the time taken to generate random numbers using random.seed(), the random.getstate() and random.setstate() functions.
import random
import timeit
# Measure the time taken to generate random numbers using seed()
t1 = timeit.timeit(stmt="""random.seed(42)
random.randint(1, 10)""", number=10000, setup="import random")
# Measure the time taken to generate random numbers using getstate() and setstate()
t2 = timeit.timeit(stmt="""random.randint(1, 10)
random.setstate(state)""", number=10000, setup="""import random
state = random.getstate()""")
print("Time taken using seed():", t1)
print("Time taken using getstate() and setstate():", t2)
Following is an output of the above code −
Time taken using seed(): 0.11702990002231672 Time taken using getstate() and setstate(): 0.06300339999143034