Python 中的 random.setstate() 函数用于将生成器的内部状态恢复到调用 getstate() 时的状态。state 参数应该是从之前对 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 状态。此后,使用 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]
[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]
[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
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
Time taken using setstate() and setstate(): 0.06645569996908307