Probability of a coin being two-headed given it lands on heads There seems to be some disagreement on among answers on the internet for the question:
A bag contains 1 fair and 1 double-sided (heads) coin. We choose a coin at random and flip it once. What is the probability of the coin being the double-sided sided one, given the result is heads?
I decided to explore the problem using Python, and came up with the code below. Does the code correctly represent the situation, and is the experimental answer of "roughly two thirds" which it produces correct please?
import random

num_trials = 10000

results = {
    "fair, heads": 0,
    "two-headed, heads": 0
}

for i in range(num_trials):
    this_coin = random.choice(["fair", "two-headed"])
    if this_coin == "fair":
        if random.choice(["heads", "tails"]) == "heads":
            results["fair, heads"] += 1
    else:
        results["two-headed, heads"] += 1
    
fair_heads =  results["fair, heads"]
double_sided_heads = results["two-headed, heads"]
print("fair, heads: " , fair_heads )
print("two-headed, heads: ", double_sided_heads)
print("experimental probability of two-headed coin given heads:", double_sided_heads, "/", double_sided_heads + fair_heads )
print("experimental probability of two-headed coin given heads:", double_sided_heads/ (double_sided_heads + fair_heads) )

 A: I would like to add to @Dave’s excellent answer by stating it in words, as some may find that easier to understand (but it’s no different).
You start with 2 coins, one is double heads, the other is normal. So that’s 4 sides, of which 3 are heads and 1 is tails. Therefore, before you have chosen a coin, your probability of getting a head is 3/4. And your probability of getting the unfair coin is 1/2.
After you have selected a coin and looked at one side to reveal a head, you have now reduced the “in play” possibilities to 3 sides: 2 heads and 1 tail.
Obviously, now the probability the other side is heads = the probability the coin is unfair. Given that, what’s the probability the other side is another head? Well, it must be 2/3.
There are lots of variations on this type of problem, often starting with 3 objects (doors/pancakes/cards) and it’s sometimes more instructive to think of those situations - indeed sometimes it helps to start with 99 unfair coins and 1 fair, and then consider the probabilities.
Richard McElreath has a nice video explaining this in the context of pancake flipping, which is a version of the Monty Hall Problem
A: The outcomes are:

*

*Fair coin, $H$


*Fair coin, $T$


*Unfair coin, $H$


*Unfair coin, $H$ (the other one)
Each of these is equally likely, so each has a probability of $1/4$, meaning that $P(\text{H}) = \frac{3}{4}$.
We want to know $
P(\text{Unfair} \vert H)
$. This is a job for Bayes' Theorem: $P(B\vert A) = \dfrac{P(A\vert B)P(B)}{P(A)}$.
Our $B$ is the unfair coin, and our $A$ is heads.
$$
P(\text{Unfair} \vert H) = \dfrac{P(H\vert\text{Unfair})P(\text{Unfair})}{P(H)} = \dfrac{1\times \frac{1}{2}}{\frac{3}{4}} = \dfrac{2}{3} 
$$
$\square$
A: Something else I would add, which might help make the answer a bit clearer: if you pull out a coin and get a tails, you put it back in the bag and start again. This is why the probability is not 50:50, because when you say you've pulled out a heads, you have encountered one of a limited set of outcomes, compared to all of the possible outcomes.
If you pulled a coin out and it was heads or tails, the probability is clearly 50:50. But you know it was heads, so it is now intuitively more likely to have been the unfair coin that was both heads.
A: This answer tries to bring together concepts from the other answers, but using clearer words.
When we “choose a coin at random and flip it once”, there are four equally likely outcomes:

*

*Fair coin, tails

*Fair coin, heads

*Two-headed coin, heads 1

*Two-headed coin, heads 2

If we know that the result is heads, we can eliminate the outcome 1, leaving outcomes 2 to 4, which are still equally likely. This gives us three equally likely outcomes, out of which two involve the two-headed coin, so the probability is 2 out of 3.
Your Python code looks perfect, and as long as it uses a good random-number generator and sufficiently many trials, will definitely produce (a good approximation to) the correct answer.
