Python 如何将种子值全局设置为特定的数字?或许这是对的?
您好,只是一个简单的问题:如何在python中设置种子值并保持这种状态?这是正确的方法吗?在我的课程开始时,只做seed(20190101)。这是否也为我的所有职能奠定了基础?我只能找到随机种子模块的答案,而不是具体的答案 调用Python 如何将种子值全局设置为特定的数字?或许这是对的?,python,random-walk,Python,Random Walk,您好,只是一个简单的问题:如何在python中设置种子值并保持这种状态?这是正确的方法吗?在我的课程开始时,只做seed(20190101)。这是否也为我的所有职能奠定了基础?我只能找到随机种子模块的答案,而不是具体的答案 调用random.seed将更改从该点向前产生的随机数序列,无论这些随机数在程序中的何处产生。或者在什么地方使用它们,比如random.choice这能回答你的问题吗@这取决于你所说的“正确”是什么意思。大多数人在每次运行程序时都会尝试用不同的号码来调用它,所以每次都不会得到
random.seed
将更改从该点向前产生的随机数序列,无论这些随机数在程序中的何处产生。或者在什么地方使用它们,比如random.choice
这能回答你的问题吗@这取决于你所说的“正确”是什么意思。大多数人在每次运行程序时都会尝试用不同的号码来调用它,所以每次都不会得到相同的“随机”号码。您决定在任何其他代码出现之前在文件顶部调用它。
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")