Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python random.setstate() Function



Therandom.setstate() function in Python is used to restores the internal state of the generator to what it was at the timegetstate() was called. The state parameter should have been obtained from a previous call togetstate(). 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 restore the internal state of the random number generator to a previously captured state.

Note − 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 random.setstate() function −

random.setstate(state)

Parameters

The Python random.setstate() function accepts a single parameter −

  • state − An object capturing the current internal state of the generator, obtained from a previous call to getstate().

Return Value

The random.setstate() function does not return any value.

Example 1

Let us look at an example using pythonrandom.setstate() function.

In the following code, a list of length 15 is defined, thenrandom.setstate() captures the current state of the random number generator. Then, the code generates a list of size 10 with the current random state. Thereafter, the state is restored usingrandom.setstate(), this ensures following output list of size 5 follows the same randomness as before.

import random list=[11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]state = random.getstate()print(random.sample(list, k = 10)) random.setstate(state)print(random.sample(list, k = 5))

Following is the output of the code −

[23, 21, 18, 11, 25, 16, 19, 22, 24, 13][23, 21, 18, 11, 25]

Example 2

In this example we will use therandom.setstate() function to restore the state of the random number generator to a previously captured state.

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 state using the setstate() functionrandom.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 3

In this example, we'll demonstrate how to use therandom.setstate() function to reproduce the same random number by capturing and restoring the state of the random number generator.

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 4

Here is another example that compare the time taken to generate random numbers usingrandom.seed(), therandom.setstate() 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 setstate() 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 setstate() and setstate():", t2)

Following is an output of the above code −

Time taken using seed(): 0.12103769998066127Time taken using setstate() and setstate(): 0.06645569996908307
python_modules.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp