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

Hello, just a quick question: how do I set a seed value in python and keep it that way? Is this the correct way to do it? By just doing seed(20190101) at the beginning of my program. Will this set the seed for all of my functions as well? I could only find answers to the random seed module, not the specific one.

Oklol2958
  • 21
  • 1
  • 8
  • Does this answer your question? [set random seed programwide in python](https://stackoverflow.com/questions/11526975/set-random-seed-programwide-in-python) – Der_Reparator Nov 18 '19 at 23:06

1 Answers1

0

Calling random.seed will change the sequence of random numbers produced from that point forward, no matter where in the program those random numbers are produced. Or for that matter where they're used, such as random.choice.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • @Oklol2958 it depends on what you mean by "correct". Most people will try to call it with a different number each time a program is run, so you don't get the same "random" numbers every time. Your decision to call it at the top of the file before any other code seems fine. – Mark Ransom Nov 18 '19 at 23:43