Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Peter's Files
Views: 6
License: MIT
Visibility: Unlisted (only visible to those who know the link)
Kernel: Python 3 (system-wide)
import random as rand
# 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
# 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
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%)