Created
June 25, 2022 04:02
-
-
Save jzstark/70b39697486da94f50738e34e412e236 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import numpy as np | |
import random | |
# first condition: average: 4.5 | |
def crossed1(p1, p2): | |
x1, y1 = p1 | |
x2, y2 = p2 | |
# p1 should be within; | |
# p2 should be on the border or out on the other side | |
cond1 = -20 < x1 and x1 < 20 and -20 < y1 and y1 < 20 | |
cond2 = x2 <= -20 or x2 >= 20 or y2 <= -20 or y2 >= 20 | |
return cond1 and cond2 | |
# second condition | |
def crossed2(p1, p2): | |
def f(x) : return 10 - x | |
x1, y1 = p1 | |
x2, y2 = p2 | |
cond1 = y1 < f(x1) | |
cond2 = y2 >= f(x2) | |
return cond1 and cond2 | |
# third condition; average: 14 | |
def crossed3(p1, p2): | |
def within(x, y) : | |
return ((x - 2.5) / 30) ** 2 + ((y - 2.5) / 40) ** 2 < 1 | |
x1, y1 = p1 | |
x2, y2 = p2 | |
cond1 = within(x1, y1) | |
cond2 = not within(x2, y2) | |
return cond1 and cond2 | |
def movep(p): | |
directions = [(0, 10), (0, -10), (10,0), (-10, 0)] | |
(dx, dy) = random.choice(directions) | |
return (p[0] + dx, p[1] + dy) | |
def round(): | |
oldp = (0, 0) | |
t = 0 | |
while True: | |
newp = movep(oldp) | |
t += 1 | |
if crossed(oldp, newp): break | |
oldp = newp | |
return t | |
def sim(n): | |
ts = [0] * n | |
for i in range(n): | |
ts[i] = round() | |
print("Round #", i, ts[i]) | |
print(np.mean(ts), np.median(ts)) | |
sim(10000) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment