Files
test/puzzles/probability/problem.2.11.py
2018-02-22 18:05:11 +00:00

42 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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. Johnsons 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))