dimanche 29 novembre 2020

Random walk in python using matplotlib, random and numpy

import random

N = 30

x = 0
y = 0
z = 0

for _ in range(N):

    n = random.random()

    if n < 1/6:
        x += 1
    elif 1/6 <= n < 2/6:
        y += 1
    elif 2/6 <= n < 3/6:
        z += 1
    elif 3/6 <= n < 4/6:
        x -= 1
    elif 4/6 <= n < 5/6:
        y -= 1
    elif n >= 5/6:
        z -= 1

    print(f"({x},{y},{z})")  # python 3.6ism

print("squared distance = {}".format(x*x + y*y + z*z))

This is my code for simulating a random walk. I would like to make this random walk biased towards going up. How can I do that?




Aucun commentaire:

Enregistrer un commentaire