Python Random getstate() Method with Examples

random getstate() Method in Python:

The getstate() method returns an object containing the random number generator’s current state.

Use this method to capture the state, and then use the setstate() method to restore the state using the captured state.

Syntax:

random.getstate()

Parameters: This method doesn’t accept any parameters.

Examples:

Example1:

Input:

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

Output:

The random value =  90
The random value =  90

Example2:

Input:

Given list = [4, 6, 7, 9]

Output:

The random value = 7
The random value = 7

Random getstate() Method with Examples in Python

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

Approach:

  • Import random module using the import keyword.
  • Take a variable and initialize it with the random.getstate() method.
  • Store it in a variable.
  • It returns an object containing the random number generator’s current state.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Take a variable and initialize it with the random.getstate() method.
# Store it in a variable.
# It returns an object containing the random number generator's current state.
a = random.getstate()
# Print the above result.
print(a)

Output:

(3, (2147483648, 2958650141, 1960736957, 2406743242, 2338959518, 3100388841, 1559261609, 2563037375, 2413032651, 2215975886, 1233119298, 2664317713, 2518715920, 1830558827, 732301835, 2271834001, 477455944, 921190001, 2858915895, 1580594191, 2936610823, 830667092, 4267652607, 3188563536, 441884984, 3644227226, 1048416985, 2890800050, 3130673655, 2625238051, 143315499, 779359520, 3444435108, 2864567679, 909868605, 1012010189, 3423481953, 4223076182, 141792975, 3552015843, 2254426651, 3627627260, 849125639, 1338840319, 4212732878, 3901314486, 1181210278, 684170455, 1584197225, 3020481743, 2853168588, 2783295314, 139795924, 4204032875, 934190350, 4108077174, 4178893516, 635531203, 2774432906, 1552622764, 2798767114, 1502888489, 3411305313, 2327347547, 4251459828, 2139438774, 4059783449, 1667094461, 3040735816, 893811547, 3798851047, 4185862502, 4290300119, 3228889436, 2788096306, 2395234615, 3209691111, 1070621771, 4015492403, 1848205985, 2400506323, 1385218858, 432017518, 1281006427, 820871546, 1945186639, 371217988, 1728844683, 1947809014, 1346204442, 442723165, 1999068426, 748674435, 2541361738, 2101616097, 769414680, 2831793506, 3524254620, 3049409168, 268951963, 2480400780, 4134107257, 1395477400, 3208207529, 3672445503, 3042969053, 1455397993, 917486726, 1922145706, 312960687, 2844244167, 4071632580, 2736256639, 1696698640, 2024593033, 1736046284, 4086243646, 4037248239, 1270948158, 2164832490, 2111024381, 1270584048, 2382608650, 3893966371, 2084875755, 3107150917, 1675442663, 2825997409, 317702383, 1018988413, 4263608476, 3168541027, 1853724402, 1145291118, 3751201164, 3532950860, 3981155907, 860374305, 2235681923, 2469242486, 1479030325, 3322428502, 3538847454, 2373501168, 1473785534, 1900401009, 1981092293, 3360828321, 482289429, 511973115, 4226523106, 2849499981, 1531287833, 1100249872, 4099483976, 3563434329, 558857975, 125626307, 3861619247, 1444702088, 4205975899, 3378371888, 4265560375, 2713139064, 740237244, 3458131473, 1901892214, 1342841988, 4099573694, 3315194634, 643611024, 4220356547, 3201457242, 1494690431, 2092718697, 1842544099, 2598552989, 4124953268, 3782395520, 647390867, 4217717487, 4220203037, 1007377947, 955588895, 2702341482, 4286159512, 2905988446, 2879841895, 3344876835, 89543556, 3220383365, 1868030775, 3983284672, 3956984449, 2317460400, 167380551, 1771720079, 632723239, 1358564073, 2700988368, 1456709041, 693492112, 2979610345, 3392835350, 1149002431, 203160637, 1934690762, 863425861, 2849572944, 2284497373, 1944080956, 2763450977, 1922145001, 2391900062, 333992930, 143804593, 3920248640, 3305379844, 2977480379, 2607396026, 2131874869, 2145138140, 1039856457, 863538755, 628747354, 3491817509, 154418846, 708726202, 2532063751, 239244789, 2858989690, 1909129016, 551562958, 689142146, 3459240983, 3657147974, 1201513610, 3559321006, 4125253396, 2541356945, 3418921110, 3848089689, 1313908835, 780119980, 2020042542, 2488123905, 2606764201, 2327619300, 2947282177, 540600019, 3730060989, 3813975070, 3876213007, 1084348454, 1332754607, 2012756408, 3396091293, 2727579719, 1399912370, 3369527132, 39070895, 2137512711, 2397705301, 3619684312, 304257835, 1469764078, 2071876341, 2208006222, 3321184261, 1173041128, 80410882, 2196528881, 37450925, 1324727098, 938565820, 2001584763, 3445531763, 491309020, 2676250001, 3483999310, 3998026213, 2461849369, 1400917992, 2191225491, 3563047168, 1212510207, 3488893261, 4118346218, 2207458670, 610723019, 2193695526, 1656574465, 2508898313, 466950484, 3482137648, 1394424081, 1822863820, 1445585358, 3648394106, 122465902, 1080557842, 699414654, 540127462, 1438041739, 1480910531, 4049362348, 1464557834, 3282349027, 853769337, 3353260359, 1914341744, 1019045686, 1270702168, 2147663044, 1106348083, 1688025031, 1690734555, 68198436, 1542301714, 2728642266, 4018559368, 2349097565, 16986703, 3336263888, 2748219259, 24837213, 3283346282, 2945318103, 1704028596, 897286349, 2850090079, 349682702, 3436121433, 3925184939, 1003643225, 754956135, 2631670969, 2397410243, 3450445780, 2312750999, 1299829746, 2763342980, 579312767, 3309267677, 1136498792, 1191114908, 3099205926, 3532865163, 345234544, 169322608, 978276397, 2696096112, 94617221, 2395199781, 991909812, 1024226657, 2219405889, 2271433807, 830613526, 3361259827, 2835737402, 1884999903, 1132224281, 413696493, 3188730185, 2834955258, 3624376277, 1198984184, 4060062947, 3949446077, 3739235065, 1789864174, 4280528658, 2586971248, 1750948931, 2116754654, 2954515174, 413201971, 3583432039, 798521670, 2169603564, 4270760492, 183831111, 4139248474, 3255497557, 1183474278, 1913795851, 1325547141, 3677776936, 3936474002, 1744949980, 2121139632, 4223244743, 4159957234, 2417668346, 2494743507, 2688374620, 3914019604, 4205889545, 2002219821, 1782600588, 117214336, 1177160969, 2965640493, 3912747464, 3942434582, 2336600933, 402520833, 82142402, 3280186928, 3357713478, 1621510120, 3539990616, 3669223424, 1183079917, 3777658477, 1030599448, 918419720, 1502509046, 3388662706, 2083364976, 3911933145, 3847853256, 2848212599, 1474549449, 3330751778, 2521906948, 2018614729, 3916144580, 3403222447, 2952103892, 3933618104, 903065308, 1459881318, 2896864685, 1034126822, 1037081, 2769042406, 628769191, 1540693719, 229813900, 2108474252, 3826882984, 3574339597, 239833054, 455943152, 2484028634, 1881220075, 2000794893, 695313419, 828592221, 547308351, 3932696785, 2935620743, 458142605, 1870966086, 946201181, 2001340621, 318989243, 3178582038, 2079358175, 4274416183, 118499468, 370301603, 4179880278, 572221261, 1760921511, 3298056424, 3507277346, 3084512280, 405919985, 1325536693, 918511433, 1834303658, 3870376942, 3501738211, 1625963426, 348323, 2242030202, 2852399194, 1903009840, 1935058216, 1578333206, 4272914968, 4072588451, 1016293237, 11455168, 3591331250, 1194216492, 1069899244, 2350206362, 3083765718, 4256991375, 2790811626, 718307776, 1410179209, 2632985187, 585324513, 2204861409, 1282232257, 1347492993, 2187847763, 3172468152, 3237258188, 1346964986, 2656582525, 183536557, 2701483246, 2162759624, 2432829348, 943743719, 2742103927, 2939613990, 2166572670, 3006186435, 1873072507, 34183711, 2468723527, 1423146305, 1777041617, 1121418387, 3894651432, 1131983113, 3245961893, 2377445864, 3927853875, 405695582, 3138955469, 1122563736, 1440982335, 1614720491, 1668620793, 3527067702, 3196877256, 1266205401, 2986550769, 4120358291, 1687916565, 1641057278, 2744272320, 1392196246, 2071479158, 4046990526, 3219630994, 1961086429, 1050005143, 149972892, 860756166, 1852412778, 3925332087, 3128578942, 188892714, 2534576442, 1123988891, 3476163348, 2544528605, 1202973395, 3527935356, 3885621558, 4157810548, 3288956832, 1586522375, 922189393, 1324140453, 4241189222, 770650455, 600493778, 2605721103, 2518507483, 1772347558, 2479514605, 2584110305, 4087275637, 3642681286, 2906292783, 751075597, 1222970586, 1791021249, 3780292328, 460147566, 1511703923, 3245139228, 3403378617, 673327266, 1330969073, 2815509917, 1526336993, 284258121, 1907931962, 1439704745, 2744932518, 256613349, 3888237198, 4169828707, 2503468627, 4079210735, 2689086452, 3119155530, 8454111, 771423642, 1158892081, 1075216921, 3940546501, 2419067660, 2469028139, 1853751035, 2052089636, 2815255744, 2630641543, 1185441581, 2781137579, 3023782122, 1941429619, 3727081123, 3957897539, 2718042759, 3286134537, 3962321570, 3128242110, 4272396116, 424628196, 2199184920, 491543448, 714790904, 624), None)
Practical implementation

Approach:

  • Import random module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Get the state of the object using the random.getstate() method.
  • Store it in another variable.
  • Print a value at random from the given list using the random.choice() method.
  • Set the state of the object using the random.setstate() method.
  • Again print the same random value from the given list using the random.choice() 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]
# Get the state of the object using the random.getstate() method.
# Store it in a variable.
rslt_state = random.getstate()
# Print a value at random from the given list using the random.choice() method
print("The random value = ", random.choice(gvn_lst))
# Set the state of the object using the random.setstate() method.
random.setstate(rslt_state)
# Again print the same random value from the given list using the
# random.choice() method
print("The random value = ", random.choice(gvn_lst))

Output:

The random value =  90
The random value =  90

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.
  • Get the state of the object using the random.getstate() method.
  • Store it in another variable.
  • Print a value at random from the given list using the random.choice() method.
  • Set the state of the object using the random.setstate() method.
  • Again print the same random value from the given list using the random.choice() 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()))

# Get the state of the object using the random.getstate() method.
# Store it in a variable.
rslt_state = random.getstate()
# Print a value at random from the given list using the random.choice() method
print("The random value = ", random.choice(gvn_lst))
# Set the state of the object using the random.setstate() method.
random.setstate(rslt_state)
# Again print the same random value from the given list using the
# random.choice() method
print("The random value = ", random.choice(gvn_lst))

Output:

Enter some random List Elements separated by spaces = 4 6 7 9
The random value = 7
The random value = 7