random.setstate() in Python Last Updated : 17 May, 2020 Comments Improve Suggest changes Like Article Like Report 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.setstate() The setstate() method of the random module is used in conjugation with the getstate() method. After using the getstate() method to capture the state of the random number generator, the setstate() method is used to restore the state of the random number generator back to the specified state. The setstate() method requires a state object as a parameter which can be obtained by invoking the getstate() method. Example 1: Python3 1== # import the random module import random # capture the current state # using the getstate() method state = random.getstate() # print a random number of the # captured state num = random.random() print("A random number of the captured state: "+ str(num)) # print another random number num = random.random() print("Another random number: "+ str(num)) # restore the captured state # using the setstate() method # pass the captured state as the parameter random.setstate(state) # now printing the same random number # as in the captured state num = random.random() print("The random number of the previously captured state: "+ str(num)) Output: A random number of the captured state: 0.8059083574308233 Another random number: 0.46568313950438245 The random number of the previously captured state: 0.8059083574308233 Example 2: Python3 # import the random module import random list1 = [1, 2, 3, 4, 5] # capture the current state # using the getstate() method state = random.getstate() # Prints list of random items of given length print(random.sample(list1, 3)) # restore the captured state # using the setstate() method # pass the captured state as the parameter random.setstate(state) # now printing the same list of random # items print(random.sample(list1, 3)) Output: [5, 2, 4] [5, 2, 4] Comment More infoAdvertise with us Y Yash_R Follow Improve Article Tags : Python Python-random Practice Tags : python Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 7 min read Python Functions 5 min read Recursion in Python 6 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 5 min read Python Tuples 4 min read Dictionaries in Python 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 10 min read Python Exception Handling 6 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 11 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like