It took Scheherazade quite a bit of time to get the king to accept the correct answer to the last problem, but she finally succeeded."I have thought of a related problem," said Scheherazade. "Suppose we have now ten chests instead of three, and each chest has three drawers. Each of the thirty drawers contains either a diamond, an emerald, or a ruby. They are dispersed in the following manner:[Of course, D stands for diamond; E for emerald; and R for ruby. So for example, Chest 4 contains one diamond and two emeralds; Chest 7 contains two emeralds and one ruby. There are ten jewels of each of the three types, and they are distributed in all ten possible ways.]
- D D D
- D D E
- D D R
- D E E
- D E R
- D R R
- E E R
- E R R
- E E E
- R R R
"You open one of the thirty drawers at random and find a diamond. Then you open another drawer of the same chest. What is the probability that it also contains a diamond?"
I will use the power of simulation to attain a nearly perfect answer, like so:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
import random | |
chests = [ | |
['D', 'D', 'D'], | |
['D', 'D', 'E'], | |
['D', 'D', 'R'], | |
['D', 'E', 'E'], | |
['D', 'E', 'R'], | |
['D', 'R', 'R'], | |
['E', 'E', 'R'], | |
['E', 'R', 'R'], | |
['E', 'E', 'E'], | |
['R', 'R', 'R'] | |
] | |
iterations = 100000 | |
found_at_least_one_diamond = 0 | |
found_two_diamonds = 0 | |
for _ in range(iterations): | |
# Not really necessary, but I abhor the idea of that jeweler's chests being disturbed! | |
chest = copy.copy(random.choice(chests)) | |
random.shuffle(chest) | |
if chest[0] == 'D': | |
found_at_least_one_diamond += 1 | |
if chest[0:2] == ['D', 'D']: | |
found_two_diamonds += 1 | |
print(found_two_diamonds / found_at_least_one_diamond) |
The most recent run outputting 0.5020185586888407 was extremely close to the analytical solution given by the author of ½. Monte Carlo simulations are really something else.
No comments:
Post a Comment