如何在全局范围内将种子值设置为特定数字?或者也许这是正确的


import statistics as stat
from statistics import mean
from random import seed, choice
from math import hypot
import random
from turtle import *
import statistics
from statistics import stdev
seed(20190101)
def random_walk(n):
x = 0
y = 0
for i in range(n):
step = random.choice(["N","S","E","W"])
if step == 'N':
y = y + 1
elif step == "S":
y = y - 1
elif step == "E":
x = x + 1
else:
x = x - 1
return (x,y)

all_steps = []
for i in range(50):
walk = random_walk(100)
all_steps.append(abs(walk[0]) + abs(walk[1]))
steps_mean = statistics.mean(all_steps) #Only after the loop
steps_max = max(all_steps)
steps_min = min(all_steps)
steps_variance = statistics.stdev(all_steps)
print("Max is",steps_max)
print("Mean is",steps_mean) 
print("Min is",steps_min)
print("variance is",steps_variance)
print("Pa random walk of 100 steps")


for i in range(50):
walk = random_walk(1000)
all_steps.append(abs(walk[0]) + abs(walk[1]))
steps_mean = statistics.mean(all_steps) #Only after the loop
steps_max = max(all_steps)
steps_min = min(all_steps)
steps_variance = statistics.stdev(all_steps)
print("Max is",steps_max)
print("Mean is",steps_mean) 
print("Min is",steps_min)
print("variance is",steps_variance)
print("Pa random walk of 1000 steps")

你好,只是一个简单的问题:如何在python中设置种子值并保持这种状态?这是正确的方法吗?在我的项目开始时只做种子(20190101(。这会为我的所有功能埋下种子吗?我只能找到随机种子模块的答案,而不能找到具体的答案。

调用random.seed将更改从该点向前生成的随机数的序列,无论这些随机数在程序中的何处生成。或者在哪里使用它们,例如random.choice

相关内容

最新更新