CoCalc Public FilesNew Projects and Problems / Blind Sailor.ipynb
Author: Peter Francis
In [1]:
import random as rand

In [2]:
# takes a coordinate and returns True if on Jetty, False otherwise
def onTheJetty(LR,UD):
if (0 <= LR) and (LR <= 20) and (0 <= UD) and (UD <= 50):
return True

In [3]:
# Runs one simulation of walking to the boat -- returns True if she makes it and false otherwise
def makesIt():
LR = 10
UD = 0
numSteps = 0

while onTheJetty(LR,UD):
# step
r = rand.uniform(0,1)
if r < 0.6:
UD = UD + 1
elif r < 0.8:
LR = LR - 1
else:
LR = LR + 1

if UD == 50:
return True # makes it
numSteps = numSteps + 1
if numSteps == 10_000:
return False # dies of thirst

return False # falls off jetty

In [4]:
nbrOfWalks = int(input('Enter number of walks: '))
numMakesIt = 0
numFails = 0
for _ in range(nbrOfWalks):
if makesIt():
numMakesIt = numMakesIt + 1
print(f'makes it : {numMakesIt} out of {nbrOfWalks} ({numMakesIt * 100 /nbrOfWalks}%)')

Enter number of walks:
makes it : 88432 out of 100000 (88.432%)