Skip to content

Commit

Permalink
Prettified the Revolver Riddle
Browse files Browse the repository at this point in the history
  • Loading branch information
Karapost committed Jul 5, 2024
1 parent bbdc235 commit 5ab78a9
Show file tree
Hide file tree
Showing 2 changed files with 264 additions and 346 deletions.
190 changes: 99 additions & 91 deletions _posts/2024-07-04-revolver-probability.md
Original file line number Diff line number Diff line change
@@ -1,86 +1,92 @@
---
layout: post
title: Probability and Survival
title: Loaded Revolver and Probability
date: 2024-07-04 08:57:00-0400
description: Answering a simple probability riddle with some code
description: Answering a riddle on probability and running some simulations.
tags: code, probability
categories: code
giscus_comments: true
giscus_comments: false
related_posts: false
---

Recently I got an quiz as first step for a ML interview. I had 40 questions over several aspects of Machine Learning, Deep Learning, Probability, and simple Practical Intuition for Model Training/Evaluation.
I had 1 minute each for each question so I was pretty under time constraints. However there is a riddle in the interview that stuck with me:
# Loaded Revolver and Probability
Recently, I took a quiz as the first step of a machine learning interview. The quiz consisted of several questions covering various aspects of machine learning, deep learning, probability, and practical intuition for model training and evaluation. One particular question, however, stuck with me:


You are given a revolver with six slots. There are two adjacent bullets. You have to
> You are given a revolver with six slots. There are two adjacent bullets. You have to
shoot twice and are given the chance to rotate the cylinder randomly in-between. How
do you maximize your chance of survival?
> - [ ] Rotate cylinder
> - [ ] Do not rotate cylinder
> - [ ] It does not matter, as the survival chance is the same in both cases
> - [ ] The question does not give all information required to answer the question
- [ ] Rotate cylinder
- [ ] Do not rotate cylinder
- [ ] It does not matter, as the survival chance is the same in both cases
- [ ] The question does not give all information required to answer the question
Under pressure, I chose to rotate the cylinder. After all, it generally made sense at the time. By resetting your chances after the first shot, it seemed like the better choice—or so I thought.

Under pressure I selected to rotate the cylinder. After all, it generally makes sense. You reset your chances after you do the first shot. Surely this would be better choice... or maybe I thought.
After the interview, I decided to delve deeper. I did some math and conducted a few experiments to verify if my thinking was correct.

After the interview I simply did some math and some additional experiments to check if my thinking was straight:
## Settings
First, let's think about the loaded revolver as a simple array that loops back on itself:

## Loaded Gun
First of all, let's think about the loaded revolver as a simple array that 'wraps around':
$$[1,1,0,0,0,0]$$

$$ [1,1,0,0,0,0] $$
where the '1' indicate the bullet and '0' an empty chamber (since the array wraps around it is irrelevant where I place the two ones as long as they are close).
Here, '1' represents a bullet, and '0' represents an empty chamber. Since the array loops back, it doesn't matter where the two bullets ('1's) are placed as long as they are next to each other.

## Rotating the Cylinder
## Option 1: Rotating the Cylinder
If you decide to rotate the cylinder between shots, the two shots become independent of each other. Using the formula for independent events, we know that:

If you decide to rotate the cylinder between, the two shots becomes independent from each other.
Using the formula of independence we know that:
$$ P(\text{surviving}) = P(\text{surviving 1st shot}) * P(\text{surviving 2nd shot}) = P(\text{surviving a shot})^2$$

This equality holds because the probability of surviving each shot is the same, regardless of the order.

$$ P(surviving) = P(surviving #1 shot) * P(surviving #2 shot) = P(surviving a shot)^2$$
Now, the probability of surviving a single shot is simply the number of empty chambers divided by the total number of chambers:

where the last equality comes from the fact that it doesn't matter which shot is the first or the second, so the probabiltiies are actually the same.
$$ P(\text{surviving a shot}) = \frac{4}{6} = \frac{2}{3} $$

Now the probability of surviving a shot is simply how many empty chambers are there compared to the total:
Therefore, the probability of surviving both shots is:

$$ P(surviving a shot) = \frac{4}{6} = \frac{2}{3} $$
which tells us that
$$ P(surviving) = (\frac{2}{3})^2 = \frac{4}{9} \approx 0.444 $$
$$ P(\text{surviving}) = (\frac{2}{3})^2 = \frac{4}{9} \approx 0.444 $$

This result shows that there is only a 44.4% chance of surviving both shots, despite there being four empty chambers. This lower probability arises because repeating the experiment (pulling the trigger) multiple times increases the overall risk.
If you decide to rotate the cylinder between, the two shots becomes independent of each other.
Using the formula of independence we know that:

which is not that much considering there are 4 empty chambers. However, since we repeat the experiment (pulling the trigger) multiple times, we are slightly more likely to shoot ourselves.
## Option 2: Not Rotating the Cylinder

## Not Rotating the Cylinder
There are two ways to compute the probability of surviving. The easiest, in my opinion, is to focus on where the trigger ends up pointing before the first shot. Since we don't rotate the cylinder between shots, everything depends on that initial position. We need to consider all possible configurations of where the trigger ends up. Using our list representation that wraps around, let's compute all possible "shifts" of the entries:

There are two way we can compute the probabiltiy here. The easiest, imo, is just focusing on the probability of where the trigger end up pointing before the first shot.
Since we don't rotate, everything depends on that! So we just need to consider all the possible configuration of where the trigger ends up. Going back to the list representation that wraps around, let's compute all possible "shifting" of the entries:
- [1,1,0,0,0,0]
- [1,0,0,0,0,1]
- [0,0,0,0,1,1]
- [0,0,0,1,1,0]
- [0,0,1,1,0,0]
- [0,1,1,0,0,0]
So we have 6 possibile cases (notice that we could have just have counted how many chambers could be in the 1st place).

If we assume that we shoot the first two entries of the list. Notice that we would die in the first two cases (because of the first shot) and in the last case (because of the second shot).
So we have 6 possible cases (note that we could have simply counted how many chambers could be in the first place).

If we assume that we shoot the first two entries of the list, we can see that we would die in the first two cases (because of the first shot) and in the last case (because of the second shot).

Thus, the probability of surviving is:

$$P(\text{surviving}) = \frac{3}{6} = \frac{1}{2} = 0.5$$

So the probability of suriviving is then:
** This is actually higher than rotating the cylinder!**

$$P(suriviving) = \frac{3}{6} = \frac{1}{2} = 0.5$$
We could have arrived at the same result by considering the second shot as dependent on the first one:

**which is actually higher than rotating the cylinder**!
$$P(\text{surviving}) = P(\text{surviving 1st shot}) P(\text{surviving 2nd shot | having survived 1st shot})$$

We could have arrived at the same results considering the second shot being dependent on the first one:
The probability of surviving the first shot is $ \frac{2}{3}$ as we saw before. But what about the second one? Well, if we survived the first shot, it means that we landed on one of the 4 empty chambers. Looking at the lists above, among the 4 zeros, there is only one that is followed by a 1 (a bullet). Thus:

$$P(surviving) = P(surviving #1 shot) P(surviving #2 shot | having survived #1 shot)$$
$$ P(\text{surviving 2nd shot | having survived 1st shot}) = \frac{3}{4} $$

So, the overall probability of surviving is:

$$ P(\text{surviving}) = \frac{2}{3} * \frac{3}{4} = \frac{1}{2}$$

Now, the first probability is equal to $ \frac{2}{3}$ as we saw before.
But what about the second one? Well, if we survived the first shot, it means that we got one of the 4 empty chambers.
If we look at the lists up there, among the 4 zeros, there is only one that is then followed by a 1 which will lead to a bullet! Thus:
$$ P(surviving #2 shot | having survived #1 shot) = \frac{3}{4} $$
$$ P(surviving) = \frac{2}{3} * \frac{3}{4} = \frac{1}{2}$$
Which is what we expected.

Below I also had some simulations just to be sure I was not assuming something wrong:
For fun, I also coded some simple probability simulation to check the results:

{::nomarkdown}
{% assign jupyter_path = "assets/jupyter/LoadedGunProbability.ipynb" | relative_url %}
Expand All @@ -93,52 +99,54 @@ Below I also had some simulations just to be sure I was not assuming something w
{% endif %}
{:/nomarkdown}

Lastly, let's ask ChatGPT4o

To determine the best strategy for maximizing your chance of survival when shooting a revolver with two adjacent bullets, let's analyze the probabilities for both options: rotating the cylinder or not rotating it.

### Not Rotating the Cylinder

If you do not rotate the cylinder after the first shot:

1. There are 6 slots and 2 adjacent bullets, so there are 4 slots that are empty.
2. Let's assume the cylinder starts at a random position.
3. The probability that the first shot is a blank is \(\frac{4}{6} = \frac{2}{3}\).
4. If the first shot is a blank, there are 5 remaining slots with 2 adjacent bullets:
- One bullet is in the slot next to the just fired empty slot.
- The probability of hitting a bullet on the second shot (with the cylinder not rotated) is \(\frac{2}{5}\).
- Thus, the probability of surviving the second shot given that the first was a blank is \(\frac{3}{5}\).

The combined probability of survival in this case is:
\[ \frac{2}{3} \times \frac{3}{5} = \frac{6}{15} = \frac{2}{5} \]

### Rotating the Cylinder

If you rotate the cylinder randomly after the first shot:

1. There are 6 slots, with 2 adjacent bullets, and we are back to the initial random situation.
2. The probability of hitting a blank on each shot is \(\frac{4}{6} = \frac{2}{3}\).

Since the rotation resets the probabilities, the survival chances are the same for each shot, and there are no dependencies between the first and second shot.

The probability of surviving both shots (since each shot is independent due to rotation) is:
\[ \frac{2}{3} \times \frac{2}{3} = \frac{4}{9} \]

### Comparison

- **Not Rotating the Cylinder:** Probability of survival is \(\frac{2}{5} = 0.4\).
- **Rotating the Cylinder:** Probability of survival is \(\frac{4}{9} \approx 0.444\).

Thus, rotating the cylinder after the first shot gives a higher probability of survival.

### Conclusion

- [X] Rotate cylinder


## So I guess it didn't work for ChatGPT.
We can see that the mistake was here:
"4. If the first shot is a blank, there are 5 remaining slots with 2 adjacent bullets:"

It didn't fully incorporate the information that the first shot is blank. If you KNOW that the first shot is blank, there are only 4 possible next chambers as we saw before.
Since it was not taken into account -> ChatGPT didn't compute the probabilites nicely.
The simulation results confirmed the previous calculations:

- Survival rate with rotating cylinder: $$\approx 0.444$$
- Survival rate without rotating cylinder: $$0.5$$

So, again, not rotating the cylinder gives a slightly better chance of survival.

## Bonus: ChatGPT4o's Answer


> To determine the best strategy for maximizing your chance of survival when shooting a revolver with two adjacent bullets, let's analyze the probabilities for both options: rotating the cylinder or not rotating it.
>### Not Rotating the Cylinder
>If you do not rotate the cylinder after the first shot:
>
>1. There are 6 slots and 2 adjacent bullets, so there are 4 slots that are empty.
>2. Let's assume the cylinder starts at a random position.
>3. The probability that the first shot is a blank is $$\frac{4}{6} = \frac{2}{3}$$.
>4. If the first shot is a blank, there are 5 remaining slots with 2 adjacent bullets:
> - One bullet is in the slot next to the just fired empty slot.
> - The probability of hitting a bullet on the second shot (with the cylinder not rotated) is $$\frac{2}{5}$$.
> - Thus, the probability of surviving the second shot given that the first was a blank is $$\frac{3}{5}$$.
>
>The combined probability of survival in this case is:
$$ \frac{2}{3} \times \frac{3}{5} = \frac{6}{15} = \frac{2}{5} $$
>
> ### Rotating the Cylinder
>
>If you rotate the cylinder randomly after the first shot:
>
>1. There are 6 slots, with 2 adjacent bullets, and we are back to the initial random situation.
>2. The probability of hitting a blank on each shot is $$\frac{4}{6} = \frac{2}{3}$$.
>
>Since the rotation resets the probabilities, the survival chances are the same for each shot, and there are no dependencies between the first and second shot.
>
>The probability of surviving both shots (since each shot is independent due to rotation) is:
>$$ \frac{2}{3} \times \frac{2}{3} = \frac{4}{9} $$
>
>### Comparison
>
>- **Not Rotating the Cylinder:** Probability of survival is $$\frac{2}{5} = 0.4$$.
>- **Rotating the Cylinder:** Probability of survival is $$\frac{4}{9} \approx 0.444$$.
>
>Thus, rotating the cylinder after the first shot gives a higher probability of survival.
>
>### Conclusion
>
>- [X] Rotate cylinder
We can see that the mistake was here: "If the first shot is a blank, there are 5 remaining slots with 2 adjacent bullets." However, since you **KNOW** that the first shot is blank, there are only 4 (and not 5) possibilities.

Always carefully check the output of generative LLMs; they are pretty good at spitting out things that might look correct but can contain errors.
Loading

0 comments on commit 5ab78a9

Please sign in to comment.