The Wayback Machine - https://web.archive.org/web/20240525002952/https://www.geeksforgeeks.org/random-setstate-in-python/
Open In App

random.setstate() in Python

Last Updated : 17 May, 2020
Improve
Improve
Like Article
Like
Save
Share
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:




# 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:




# 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]


Previous Article
Next Article

Similar Reads

Python Random - random() Function
There are certain situations that involve games or simulations which work on a non-deterministic approach. In these types of situations, random numbers are extensively used in the following applications: Creating pseudo-random numbers on Lottery scratch cardsreCAPTCHA on login forms uses a random number generator to define different numbers and ima
3 min read
Random sampling in numpy | random() function
numpy.random.random() is one of the function for doing random sampling in numpy. It returns an array of specified shape and fills it with random floats in the half-open interval [0.0, 1.0). Syntax : numpy.random.random(size=None) Parameters : size : [int or tuple of ints, optional] Output shape. If the given shape is, e.g., (m, n, k), then m * n *
2 min read
NumPy random.noncentral_f() | Get Random Samples from noncentral F distribution
The NumPy random.noncentral_f() method returns the random samples from the noncentral F distribution. Example C/C++ Code import numpy as np import matplotlib.pyplot as plt gfg = np.random.noncentral_f(1.24, 21, 3, 1000) count, bins, ignored = plt.hist(gfg, 50, density = True) plt.show() Output: Syntax Syntax: numpy.random.noncentral_f(dfnum, dfden,
1 min read
Random Numbers in Python
Python defines a set of functions that are used to generate or manipulate random numbers through the random module. Functions in the random module rely on a pseudo-random number generator function random(), which generates a random float number between 0.0 and 1.0. These particular type of functions is used in a lot of games, lotteries, or any appl
5 min read
Python | Generate random numbers within a given range and store in a list
Given lower and upper limits, Generate random numbers list in Python within a given range, starting from 'start' to 'end', and store them in the list. Here, we will generate any random number in Python using different methods. Examples: Input: num = 10, start = 20, end = 40 Output: [23, 20, 30, 33, 30, 36, 37, 27, 28, 38] Explanation: The output co
5 min read
Python implementation of automatic Tic Tac Toe game using random number
Tic-tac-toe is a very popular game, so let's implement an automatic Tic-tac-toe game using Python. The game is automatically played by the program and hence, no user input is needed. Still, developing an automatic game will be lots of fun. Let's see how to do this. NumPy and random Python libraries are used to build this game. Instead of asking the
5 min read
Generating random Id's in Python
In python there are different ways to generate id's. Let's see how different types of Id's can be generated using python without using inbuilt Python libraries. 1. Generating Random Integer as Ids' Code #1 : Print 10 random values of numbers between 1 and 100. C/C++ Code # Python3 code to demonstrate the # random generation of Integer id's import r
2 min read
Generating Random id's using UUID in Python
We had discussed the ways to generate unique id's in Python without using any python inbuilt library in Generating random Id’s in Python In this article we would be using inbuilt functions to generate them. UUID, Universal Unique Identifier, is a python library which helps in generating random objects of 128 bits as ids. It provides the uniqueness
3 min read
uniform() method in Python Random module
uniform() is a method specified in the random library in Python 3. Nowadays, in general, day-day tasks, there's always the need to generate random numbers in a range. Normal programming constructs require a method more than just one word to achieve this particular task. In python, there's an inbuilt method, "uniform()" which performs this task with
2 min read
Python | Select random value from a list
Given a list and our task is to randomly select elements from the list in Python using various functions. Selecting random numbers from a list can be used sometimes while building games, choosing a random range, etc. Example Input: [2, 3, 4 , 5, 6 ] Output: 2 Explaination:The output we are getting is a random value from the input listSelect the Ran
5 min read
Article Tags :
Practice Tags :