From fc452797281ba9006360af9cdcde12c17fd4477d Mon Sep 17 00:00:00 2001 From: Vahagn Khachatryan Date: Thu, 22 Feb 2018 18:05:11 +0000 Subject: [PATCH] Probability Theory Problem 2.11 --- puzzles/probability/problem.2.11.py | 41 +++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 puzzles/probability/problem.2.11.py diff --git a/puzzles/probability/problem.2.11.py b/puzzles/probability/problem.2.11.py new file mode 100644 index 0000000..27065cf --- /dev/null +++ b/puzzles/probability/problem.2.11.py @@ -0,0 +1,41 @@ + +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))