-
Notifications
You must be signed in to change notification settings - Fork 8
/
random-walk-R3.py
45 lines (39 loc) · 1.09 KB
/
random-walk-R3.py
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
# Copyright (c) 2017 N.P. Rougier and F.C.Y. Benureau
# Release under the BSD 2-clause license
# Tested with 64-bit CPython 3.6.2 / macOS 10.12.6
import sys, subprocess, datetime, random
def compute_walk():
x = 0
walk = []
for i in range(10):
if random.uniform(-1, +1) > 0:
x += 1
else:
x -= 1
walk.append(x)
return walk
# If repository is dirty, don't run anything
if subprocess.call(("git", "diff-index",
"--quiet", "HEAD")):
print("Repository is dirty, please commit first")
sys.exit(1)
# Get git hash if any
hash_cmd = ("git", "rev-parse", "HEAD")
revision = subprocess.check_output(hash_cmd)
# Unit test
random.seed(42)
assert compute_walk() == [1,0,-1,-2,-1,0,1,0,-1,-2]
# Random walk for 10 steps
seed = 1
random.seed(seed)
walk = compute_walk()
# Display & save results
print(walk)
results = {
"data" : walk,
"seed" : seed,
"timestamp": str(datetime.datetime.utcnow()),
"revision" : revision,
"system" : sys.version}
with open("results-R3.txt", "w") as fd:
fd.write(str(results))