Python 本地随机种子

huangapple go评论56阅读模式
英文:

Python Local random seed

问题

在函数内部设置局部随机种子并且不影响函数外部的随机状态是可能的。您可以通过保存和恢复全局随机状态来实现这一目标。下面是您提供的代码的修改版本,以使函数在不影响函数外部的随机状态的情况下生成相同的随机数:

import numpy as np

def random():
    local_state = np.random.get_state()  # 保存当前的随机状态
    np.random.seed(420)  # 在函数内部设置局部随机种子
    np.random.randint(1, 100)  # 生成随机数
    np.random.randint(1, 100)  # 生成随机数
    np.random.set_state(local_state)  # 恢复之前保存的随机状态
    return None

np.random.seed(69)
for n in range(3):
    np.random.randint(1, 100)  # 输出:55,76,74
random()
for n in range(3):
    np.random.randint(1, 100)  # 输出:91,56,21

通过使用np.random.get_state()保存当前的随机状态,并使用np.random.set_state()恢复之前保存的随机状态,您可以确保函数内的随机数生成不会影响函数外的随机状态。

英文:

I have a random seed set at the start of my run for reproducibility. But there are a few sub-functions (e.g. random) that also use random numbers. If I used a different random number seed just for those, it affects the random seed outside of the function. Is it possible to set the random seed and use it only locally inside the function and the random state outside the function does not get affected? I believe I can always get the random state, save it and restore it. Would there be an easier option? I showed an example below.

import numpy as np

def random():
    np.random.seed(420)
    np.random.randint(1, 100)
    np.random.randint(1, 100)
    return None


np.random.seed(69)
for n in range(3):
    np.random.randint(1,100) # outputs : 55,76,74
for n in range(3):
    np.random.randint(1,100) # outputs : 91,56,21

Is it possible to make the function below also output the same thing?

np.random.seed(69)
for n in range(3):
    np.random.randint(1,100) # outputs : 55,76,74
random()
for n in range(3):
    np.random.randint(1,100) # would like it to output : 91,56,21

答案1

得分: 3

这就是为什么有numpy随机生成器,也是为什么他们建议使用它。只需为每个实例定义一个生成器,例如:

def rando(rng):
    print('function')
    print(rng.integers(1, 100))
    print(rng.integers(1, 100))
    print('end of function')
    return None

rng1 = np.random.default_rng(69)
rng2 = np.random.default_rng(420)

for n in range(3):
    print(rng1.integers(1, 100)) # 输出: 6,58,67
rando(rng2) # 输出 62, 77
for n in range(3):
    print(rng1.integers(1, 100)) # 希望输出: 53,78,86

产生的结果:

6
58
67
function
62
77
end of function
53
78
86

当您注释掉函数调用时,您会得到:

6
58
67
53
78
86
英文:

That's why there are numpy random generators and that is why they recommend using that. Just define one generator for each instance, e.g.:

def rando(rng):
    print('function')
    print(rng.integers(1, 100))
    print(rng.integers(1, 100))
    print('end of function')
    return None



rng1 = np.random.default_rng(69)
rng2 = np.random.default_rng(420)

for n in range(3):
    print(rng1.integers(1, 100)) # outputs : 6,58,67
rando(rng2) # outputs 62, 77
for n in range(3):
    print(rng1.integers(1, 100)) # would like it to output : 53,78,86

yielding:

6
58
67
function
62
77
end of function
53
78
86

and when you comment out the function call, you get:

6
58
67
53
78
86

huangapple
  • 本文由 发表于 2023年6月1日 20:49:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定