import random # Problem: # 2.11 # At a completely random moment between 6:30 and 7:30 a.m., # the morning newspaper is delivered to Mr. Johnson’s residence. # Mr. Johnson leaves for work at a completely random moment # between 7:00 and 8:00 a.m. regardless of whether the newspaper # has been delivered. What is the probability that Mr. Johnson can # take the newspaper with him to work? Use computer simulation to # find the probability. # # Solution: # Applying Bayes' theorem. # Newsletter arrives in interval 6:30 - 7:00 ? # yes| p=0.5 no| p=0.5 # | Mr. Jonhnson leaves in interval 7:00 - 7:30 # | yes| p=0.5 no| p=0.5 # | Newsletter arrives first in | # | interval 7:00-7:30 | # | yes| p=0.5 no| p=0.5 | # | | | # 0.5 + 0.5*0.5*0.5 + 0.5*0.5 = 0.875 # # Simulation: # 0.874867 # 0.874876 # 0.874876 # newspaper_arrived_on_time = 0 samples = 10000000 for i in range(samples): n = random.random() w = random.random() + 0.5 if w >= n: newspaper_arrived_on_time += 1 print('simulated probability of ontime arrived newspaper is : {}' .format(newspaper_arrived_on_time/samples))