Python - AI 助手

Python random.setstate() 函式



Python 中的 **random.setstate()** 函式用於將生成器的內部狀態恢復到呼叫 **getstate()** 時所處的狀態。狀態引數應從之前對 **getstate()** 的呼叫中獲得。此函式是 random 模組的一部分,該模組提供各種函式來生成隨機數和序列。

此函式的主要目的是將隨機數生成器的內部狀態恢復到先前捕獲的狀態。

**注意** − 此函式不能直接訪問,因此我們需要匯入 random 模組,然後需要使用 random 靜態物件呼叫此函式。

語法

以下是 random.setstate() 函式的語法:

random.setstate(state)

引數

Python random.setstate() 函式接受單個引數:

  • **state** − 一個物件,捕獲生成器的當前內部狀態,該狀態是從之前對 getstate() 的呼叫中獲得的。

返回值

random.setstate() 函式不返回值。

示例 1

讓我們來看一個使用 Python **random.setstate()** 函式的示例。

在下面的程式碼中,定義了一個長度為 15 的列表,然後 **random.setstate()** 捕獲隨機數生成器的當前狀態。然後,程式碼使用當前隨機狀態生成一個大小為 10 的列表。此後,使用 **random.setstate()** 恢復狀態,這確保後續大小為 5 的輸出列表與之前具有相同的隨機性。

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)) 

以下是程式碼的輸出:

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

示例 2

在這個例子中,我們將使用random.setstate()函式來恢復隨機數生成器的狀態到之前捕獲的狀態。

import random

# Initialize the random number generator
random.seed(42)

# Generate a sample of 10 numbers from a range of 20
print(random.sample(range(30), k=10))

# Capture the current state
state = random.getstate()

# Generate a sample of 20 numbers from a range of 20
print(random.sample(range(20), k=20))

# Restore the state using the setstate() function
random.setstate(state)

# Generate another sample of 10 numbers from the same range
print(random.sample(range(20), k=10))

執行上述程式後,會產生以下結果:

[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]

示例 3

在這個例子中,我們將演示如何使用random.setstate()函式透過捕獲和恢復隨機數生成器的狀態來重現相同的隨機數。

import random

# Initialize the random number generator and get state
random.seed(0)
initial_state = random.getstate()

# Generate and print random number
print(random.random())

print(random.random())

# Setting the seed back to 0 resets the RNG back to the original state
random.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 explicitly
random.setstate(initial_state)
print(random.random())

上述程式碼的輸出如下:

0.8444218515250481
0.7579544029403025
0.8444218515250481
0.8444218515250481

示例 4

這是一個比較使用random.seed()random.setstate()random.setstate()函式生成隨機數所需時間的另一個示例。

import random
import 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 random
state = random.getstate()""")

print("Time taken using seed():", t1)
print("Time taken using setstate() and setstate():", t2)

以下是上述程式碼的輸出:

Time taken using seed(): 0.12103769998066127
Time taken using setstate() and setstate(): 0.06645569996908307
python_modules.htm
廣告