walk.py 479 Bytes
Newer Older
cermak's avatar
cermak committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/python3

import random

N = 10
max_steps = 99999999
repeats = 10000

def go():
    poloha = 0

    for i in range(max_steps):
        krok = random.choice([-1,1])
        poloha = max(0, poloha + krok) # hranice na nule
        #poloha = abs(poloha + krok)    # pruchod nulou
        if poloha == N:
            return i

total_steps = 0
for _ in range(repeats):
    total_steps += go()

print(f"Průměrně jsem do polohy {N} došel za {total_steps/repeats} kroků.")