Visibility: Unlisted (only visible to those who know the link)
Kernel: Python 3 (system-wide)
In [1]:
importrandomasrand
In [2]:
# takes a coordinate and returns True if on Jetty, False otherwisedefonTheJetty(LR,UD):if(0<=LR)and(LR<=20)and(0<=UD)and(UD<=50):returnTrue
In [3]:
# Runs one simulation of walking to the boat -- returns True if she makes it and false otherwisedefmakesIt():LR=10UD=0numSteps=0whileonTheJetty(LR,UD):# stepr=rand.uniform(0,1)ifr<0.6:UD=UD+1elifr<0.8:LR=LR-1else:LR=LR+1ifUD==50:returnTrue# makes itnumSteps=numSteps+1ifnumSteps==10_000:returnFalse# dies of thirstreturnFalse# falls off jetty
In [4]:
nbrOfWalks=int(input('Enter number of walks: '))numMakesIt=0numFails=0for_inrange(nbrOfWalks):ifmakesIt():numMakesIt=numMakesIt+1print(f'makes it : {numMakesIt} out of {nbrOfWalks} ({numMakesIt*100/nbrOfWalks}%)')