This problem deals with continuous (rather than discrete) probability, but it's an interesting problem! It involves probabilistically estimating the value of (pie). Consider a circle of radius 1 inscribed within a square with side 2. Both shapes are centered at the origin (0, 0).
Using basic geometry, the ratio of the circle's area to the square's area is(pie) (1)2/22 = (pie)/4.
Now, suppose that you randomly throw some darts at this figure. Out of n total attempts, m attempts land within the circle. As the number of attempts becomes large, the ratio m/n should approach the ratio of the circle's area to the square's area. Thus, we can write (pie)/4 = m/n. Solving for(pie) gives us (pie) = 4m/n.
Write a Python program that allows the user to enter a value for n (the total number of attempts). Your program should then simulate throwing n darts at the figure by randomly picking coordinates (x, y) between -1.0 and 1.0. Keep track of the darts that land within the circle, and show the resulting estimate for(pie) .
Python hint: Python's random() function (located in the random module) behaves the same as Java's Math.random() - it returns a uniformly distributed pseudorandom real number in the interval [0.0, 1.0). To call it:
import random
x = random.random()
2. The Monty Hall problem is a counterintuitive example of how probability works. Here's the premise: You are on a game show where you have to pick one of three closed doors. Behind one of these doors is a new car, and behind the other two are goats. Once you've picked a door, the show's host (who knows the winning door) opens one of the other doors to reveal a goat and asks if you'd like to change your choice. To maximize your chances of winning the car, should you change or stay with your original door?
The probability that you originally picked the winning door is obviously 1/3. This means that the probability you originally picked a losing door is 2/3. However, if you originally picked a losing door and change your choice after the host opens the other losing door, you are guaranteed a win!
(Remember that the door opened by the host is always a losing door.) Thus, if you always change your choice, the probability that you win is the same as the probability that you initially picked
a losing door. This is 2/3 - much better than sticking with your original choice!
(Of course, all this analysis does assume that you'd prefer a new car over a goat...)
Write a Python simulation to experimentally verify the Monty Hall problem. Your simulation should run a large number of trials for two scenarios: staying with the original door, and changing the door. For each scenario, report the number and percentage of wins over all of the trials.
Simulation algorithm (for each trial) for staying with the original door:
a. Randomly place the car
b. Have the player randomly choose one of the three doors
c. Determine whether the player won
Note that the above scenario does not require accounting for which door is opened, since the player is not going to alter his/her original choice.
Simulation algorithm (for each trial) for changing the door:
d. Randomly place the car
e. Have the player randomly choose one of the three doors
f. Open a losing door (besides the player's original choice)
g. Change the player's choice to the one remaining door
h. Determine whether the player won
Python hint: Python's randint(a, b) function (also located in the random module) returns a random integer N such that a (less than or equal to) N (less than or equal to) b.