Python Random setstate() Method with Examples

random setstate() Method in Python:

The setstate() method is used to return the random number generator’s state back to the specified state.

To capture the state, use the getstate() method.

Syntax:

random.setstate(state)

Parameters

state: This is Required. It is a state object. The setstate() method returns the random number generator’s state to this state.

Examples:

Example1:

Input:

Given list = [10, 40, 60, 80, 90]
Given length = 2

Output:

[10, 80]
[10, 80]

Example2:

Input:

Given list = [25, 35, 9, 45, 65]
Given length = 3

Output:

[45, 35, 65]
[45, 35, 65]

Random setstate() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import random module using the import keyword.
  • Using the random() function print a random number.
  • Capture the state of the object using the random.getstate() method.
  • Using the random() function print the other random number.
  • Restore back the state of the object using the random.setstate() method.
  • Again print the same random value when the state was captured using the random() function.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random

# Using the random() function print a random number.
print(random.random())

# Capture the state of the object using the random.getstate() method
rslt_state = random.getstate()

# Using the random() function print the other random number.
print(random.random())

# Restore back the state of the object using the random.setstate() method
random.setstate(rslt_state)
# Again print the same random value when the state was captured using the random()
# function
print(random.random())

Output:

0.05457533761313371
0.8804207213354224
0.8804207213354224
Practical implementation

Approach:

  • Import random module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Using the getstate() method, you can capture the current state.
  • Store it in another variable.
  • Print a list of random items of the specified length using the random.sample() method.
  • Using the setstate() method, you can restore the captured state by passing the above state as a parameter to the random.setstate() method.
  • Print the same random list of items using the random.sample() method.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the list as static input and store it in a variable.
gvn_lst = [10, 40, 60, 80, 90]
# Using the getstate() method, you can capture the current state.
# Store it in another variable.
rslt_state = random.getstate()

# Print a list of random items of the specified length using the random.sample()
# method.
print(random.sample(gvn_lst, 2))

# Using the setstate() method, you can restore the captured state by passing the
# above state as a parameter to the random.setstate() method.
random.setstate(rslt_state)

# Print the same random list of items using the random.sample() method.
print(random.sample(gvn_lst, 2))

Output:

[10, 80]
[10, 80]

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import random module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Using the getstate() method, you can capture the current state.
  • Store it in another variable.
  • Print a list of random items of the specified length using the random.sample() method.
  • Using the setstate() method, you can restore the captured state by passing the above state as a parameter to the random.setstate() method.
  • Print the same random list of items using the random.sample() method.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))

# Using the getstate() method, you can capture the current state.
# Store it in another variable.
rslt_state = random.getstate()

# Print a list of random items of the specified length using the random.sample()
# method.
print(random.sample(gvn_lst, 3))

# Using the setstate() method, you can restore the captured state by passing the
# above state as a parameter to the random.setstate() method.
random.setstate(rslt_state)

# Print the same random list of items using the random.sample() method.
print(random.sample(gvn_lst, 3))

Output:

Enter some random List Elements separated by spaces = 25 35 9 45 65
[45, 35, 65]
[45, 35, 65]