Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python random.getstate() Function



Therandom.getstate() function in Python is used to retrieve an object capturing the current internal state of the random number generator. This object can later be passed to thesetstate() method to restore the generator to this state. This function is part of the random module, which provides various functions to generate random numbers and sequences.

The primary purpose of this function is to capture the state of the generator at a specific moment, and later restore this state to effectively reproduce the same random values.

This function is not accessible directly, so we need to import the random module and then we need to call this function using random static object.

Syntax

Following is the syntax of the Pythonrandom.getstate() function −

random.getstate()

Parameters

This function does not accept any parameters.

Return Value

The random.getstate() function returns an object containing the current internal state of the generator.

Example

Let's look at an example of how to use therandom.getstate() function to capture and restore the state of the random number generator.

import random# Initialize the random number generatorrandom.seed(42)# Generate a sample of 10 numbers from a range of 20print(random.sample(range(30), k=10))# Capture the current statestate = random.getstate()# Generate a sample of 20 numbers from a range of 20print(random.sample(range(20), k=20))# Restore the staterandom.setstate(state)# Generate another sample of 10 numbers from the same rangeprint(random.sample(range(20), k=10))

When we run the above program, it produces the following result −

[20, 3, 0, 23, 8, 7, 24, 4, 28, 17][2, 18, 13, 1, 0, 16, 3, 17, 8, 9, 15, 11, 12, 5, 6, 4, 7, 10, 14, 19][2, 18, 13, 1, 0, 16, 3, 17, 8, 9]

Example

In this example, we'll demonstrate how to use therandom.getstate() function to capture the state of the random number generator and then restore it to generate the same sequence of random numbers.

import random# Initialize the random number generator and get staterandom.seed(0)initial_state = random.getstate()# Generate and print random numberprint(random.random())print(random.random())# Setting the seed back to 0 resets the RNG back to the original staterandom.seed(0)new_state = random.getstate()assert new_state == initial_state# Since the state of the generator is the same as before, it will produce the same sequence print(random.random())# We could also achieve the same outcome by resetting the state explicitlyrandom.setstate(initial_state)print(random.random())

The output of the above code is as follows −

0.84442185152504810.75795440294030250.84442185152504810.8444218515250481

Example

Here is another example that compare the time taken to generate random numbers usingrandom.seed(), therandom.getstate() andrandom.setstate() functions.

import randomimport timeit# Measure the time taken to generate random numbers using seed()t1 = timeit.timeit(stmt="""random.seed(42)random.randint(1, 10)""", number=10000, setup="import random")# Measure the time taken to generate random numbers using getstate() and setstate()t2 = timeit.timeit(stmt="""random.randint(1, 10)random.setstate(state)""", number=10000, setup="""import randomstate = random.getstate()""")print("Time taken using seed():", t1)print("Time taken using getstate() and setstate():", t2)

Following is an output of the above code −

Time taken using seed(): 0.11702990002231672Time taken using getstate() and setstate(): 0.06300339999143034
python_modules.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp