{"id":25486,"date":"2021-11-16T08:40:01","date_gmt":"2021-11-16T03:10:01","guid":{"rendered":"https:\/\/python-programs.com\/?p=25486"},"modified":"2021-11-16T08:40:01","modified_gmt":"2021-11-16T03:10:01","slug":"python-random-setstate-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-random-setstate-method-with-examples\/","title":{"rendered":"Python Random setstate() Method with Examples"},"content":{"rendered":"
random setstate() Method in Python:<\/strong><\/p>\n The setstate() method is used to return the random number generator’s state back to the specified state.<\/p>\n To capture the state, use the getstate() method.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n state:<\/strong> This is Required. It is a state object. The setstate() method returns the random number generator’s state to this state.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" 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: …<\/p>\nrandom.setstate(state)<\/pre>\n
Given list = [10, 40, 60, 80, 90]\r\nGiven length = 2<\/pre>\n
[10, 80]\r\n[10, 80]<\/pre>\n
Given list = [25, 35, 9, 45, 65]\r\nGiven length = 3<\/pre>\n
[45, 35, 65]\r\n[45, 35, 65]<\/pre>\n
Random setstate() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import random module using the import keyword.\r\nimport random\r\n\r\n# Using the random() function print a random number.\r\nprint(random.random())\r\n\r\n# Capture the state of the object using the random.getstate() method\r\nrslt_state = random.getstate()\r\n\r\n# Using the random() function print the other random number.\r\nprint(random.random())\r\n\r\n# Restore back the state of the object using the random.setstate() method\r\nrandom.setstate(rslt_state)\r\n# Again print the same random value when the state was captured using the random()\r\n# function\r\nprint(random.random())\r\n<\/pre>\n
0.05457533761313371\r\n0.8804207213354224\r\n0.8804207213354224<\/pre>\n
Practical implementation<\/h5>\n
\n
# Import random module using the import keyword.\r\nimport random\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [10, 40, 60, 80, 90]\r\n# Using the getstate() method, you can capture the current state.\r\n# Store it in another variable.\r\nrslt_state = random.getstate()\r\n\r\n# Print a list of random items of the specified length using the random.sample()\r\n# method.\r\nprint(random.sample(gvn_lst, 2))\r\n\r\n# Using the setstate() method, you can restore the captured state by passing the\r\n# above state as a parameter to the random.setstate() method.\r\nrandom.setstate(rslt_state)\r\n\r\n# Print the same random list of items using the random.sample() method.\r\nprint(random.sample(gvn_lst, 2))\r\n<\/pre>\n
[10, 80]\r\n[10, 80]<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import random module using the import keyword.\r\nimport random\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n\r\n# Using the getstate() method, you can capture the current state.\r\n# Store it in another variable.\r\nrslt_state = random.getstate()\r\n\r\n# Print a list of random items of the specified length using the random.sample()\r\n# method.\r\nprint(random.sample(gvn_lst, 3))\r\n\r\n# Using the setstate() method, you can restore the captured state by passing the\r\n# above state as a parameter to the random.setstate() method.\r\nrandom.setstate(rslt_state)\r\n\r\n# Print the same random list of items using the random.sample() method.\r\nprint(random.sample(gvn_lst, 3))\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 25 35 9 45 65\r\n[45, 35, 65]\r\n[45, 35, 65]<\/pre>\n