summaryrefslogtreecommitdiff
path: root/prob/random_walk_demo.py
blob: 62125289394e25eee114e091a2fb8601049e7285 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

import random


def random_walk(N):
    x, y = 0, 0
    choices = ['N', 'S', 'E', 'W']
    for i in range(N):
        step = random.choice(choices)
        if step == 'N':
            y += 1
        elif step == 'S':
            y -= 1
        elif step == 'E':
            x += 1
        else:
            x -= 1
    return x, y


def random_walk_2(N):
    x, y = 0, 0
    for i in range(N):
        dx, dy = random.choice([[0, 1], [0, -1], [1, 0], [-1, 0]])
        x += dx
        y += dy
    return x, y


def distance_from_home(x, y):
    return abs(x) + abs(y)


if __name__ == '__main__':
    # for i in range(25):
    #     x, y = random_walk_2(10)
    #     print(f'x = {x}, y = {y}, distance from home: {distance_from_home(x, y)}')

    number_of_walks = 50000
    for walk_length in range(1, 31):
        no_transport = 0
        for i in range(number_of_walks):
            x, y = random_walk(walk_length)
            dist = distance_from_home(x, y)
            if dist <= 5:
                no_transport += 1
        no_transport_percentage = float(no_transport)/number_of_walks
        print(f'walk_length: {walk_length}, no_transport_percentage: {no_transport_percentage}')