summaryrefslogtreecommitdiff
path: root/prob/random_walk_1d.py
blob: 445db06a0c3c7ffb2cb7681cae62e7c49925cf4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

import random
import matplotlib.pyplot as plt
from collections import Counter
import pandas as pd


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


if __name__ == '__main__':
    n_samples = 50000
    final_x = []
    for i in range(n_samples):
        final_x.append(random_walk(100))
    pd.Series(final_x).value_counts().sort_index().plot(kind='bar')
    plt.show()