From 6f68e1818229e0d2dad760062e6b5bb137b88f5b Mon Sep 17 00:00:00 2001 From: zhang Date: Sun, 23 Jan 2022 12:42:38 +0800 Subject: =?UTF-8?q?=E9=9A=8F=E6=9C=BA=E6=B8=B8=E8=B5=B0=20&=20ortools=20de?= =?UTF-8?q?mo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- prob/random_walk_1d.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 prob/random_walk_1d.py (limited to 'prob/random_walk_1d.py') diff --git a/prob/random_walk_1d.py b/prob/random_walk_1d.py new file mode 100644 index 0000000..445db06 --- /dev/null +++ b/prob/random_walk_1d.py @@ -0,0 +1,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() -- cgit v1.2.3