summaryrefslogtreecommitdiff
path: root/prob/random_walk_1d.py
diff options
context:
space:
mode:
authorzhang <zch921005@126.com>2022-01-23 12:42:38 +0800
committerzhang <zch921005@126.com>2022-01-23 12:42:38 +0800
commit6f68e1818229e0d2dad760062e6b5bb137b88f5b (patch)
tree55ec8ad3f340d7a9d276b2e17641b8a9c1c52ae3 /prob/random_walk_1d.py
parent0451d59752f3b61a6f6dfdb56d1f431083be4c7d (diff)
随机游走 & ortools demo
Diffstat (limited to 'prob/random_walk_1d.py')
-rw-r--r--prob/random_walk_1d.py22
1 files changed, 22 insertions, 0 deletions
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()