random.getstate() in Python
random() module is used to generate random numbers in Python. Not actually random, rather this is used to generate pseudo-random numbers. That implies that these randomly generated numbers can be determined.
random.getstate()
The getstate() method of the random module returns an object with the current internal state of the random number generator. This object can be passed to the setstate() method to restore the state. There are no parameters passed in this method.
Example 1:
Python3
import random
state = random.getstate()
print (random.sample( range ( 20 ), k = 10 ))
random.setstate(state)
print (random.sample( range ( 20 ), k = 5 ))
|
Output:
[16, 1, 0, 11, 19, 3, 7, 5, 10, 13]
[16, 1, 0, 11, 19]
Example 2:
Python3
import random
list1 = [ 1 , 2 , 3 , 4 , 5 , 6 ]
state = random.getstate()
print (random.choice(list1))
random.setstate(state)
print (random.choice(list1))
|
Output:
3
3
Last Updated :
27 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...