This problem is a beautiful and elegant probability based on an elementary problem on how to effectively choose the key to a lock. This gives a simulation environment to problem 6 of ISI MStat 2017 PSB.

## Problem

Suppose you have a 4-digit combination lock, but you have forgotten the correct combination. Consider the following three strategies to find the correct one:

(i) Try the combinations consecutively from 0000 to 9999.

(ii) Try combinations using simple random sampling with replacement from the set of all possible combinations.

(iii) Try combinations using simple random sampling without replacement from the set of all possible combinations.

Assume that the true combination was chosen uniformly at random from all possible combinations. Determine the expected number of attempts needed to find the correct combination in all three cases.

This problem really intrigues me, which gives me the excitement to solve and solve it.

### Prerequisite

- Expectation
- Simple Random Sampling With and Without Replacement
- Discrete Uniform Distribution
- Geometric Distribution
- Smoothing of Expectation \( E_X(E(Y|X)) = E(Y)\). When \(Y\) and \(X\) have some relationship, this helps us to calculate the expectation.

## Solution

### Consecutive Combination

\(U\) ~ Discrete Uniform \(({0, 1, 2, …, 9999})\)

Suppose, observe that if you select the keys consecutively, then for the true key \(U\), you need \(U\) attempts. **(*)**

\(N\) denotes the number of attempts required = \(U + 1\) due to** (*)**

\( E(N) = E(U) = \frac{9999}{2}\).

### Simple Random Sampling With Replacement

This is something no one does, but let’s calculate this and see why we don’t do this and why we need to remember the keys that don’t work like SRSWOR, which is the next case.

\(U\) ~ Discrete Uniform \(({0, 1, 2, …, 9999})\)

\(N\) denotes the number of attempts required. \( E_U(E(N|U)) = E(N)\)

Let’s say, we have observed \(U\), which is fixed and we will calculate \(E(N|U)\).

Observe that \(N|U\) ~ Geom(\frac{1}{10000}\), since, there are unlimited trials and success occurs if you pick up the right key \(U\), which has a probability of \(\frac{1}{10000}\).

Therefore, \(E(N|U) = 10000\). Hence, \( E(N) = E_U(E(N|U)) = 10000\)

**Let’s simulate it.**

```
#Simple Random Sampling with Replacement
NUM = 0
size = 1000 # we have taken 1000 for easier calculation
key = sample(size,1)
number = NULL
random = sample(size,1)
N = 1
for (k in 1:1000) {
number = NULL
for (j in 1:100)
{
while (random != key)
{
random = sample(size,1)
N = N + 1
}
number = c(number,N)
random = sample(size,1)
N = 1
}
NUM = c(NUM,mean(number))
}
mean(NUM)
980.899
hist(NUM)
#Note Replace = TRUE will not work, since, this is an open-ended program
```

Hence, this is validated by our simulation.

### Simple Random Sampling Without Replacement

This is the sort of key selection, we usually do. Let’s investigate it.

\(U\) ~ Discrete Uniform \(({0, 1, 2, …, 9999})\)

\(N\) denotes the number of attempts required. \( E_U(E(N|U)) = E(N)\)

Let’s say, we have observed \(U\), which is fixed and we will calculate \(E(N|U)\).

\( p_i = P ((N|U) = i) = \frac{9999}{10000}.\frac{9998}{9999}.\frac{9997}{9998}…\frac{10001-i}{10002-i}.\frac{1}{10000} = \frac{10001 – i}{10000^2}\)

\(E(N|U) = \sum_{i = 1}^{10000} ip_i = \sum_{i = 1}^{10000} i \cdot \frac{10001 – i}{10000^2} = \frac{10001}{20000} \). Hence, \( E(N) = E_U(E(N|U)) = \frac{10001}{20000}\).

```
#Simple Random Sampling without Replacement
average = NULL
number = NULL
size = 10000
key = sample(size,1)
for (j in 1:1000)
{
for (i in 1:100)
{
option = sample(size,size, replace = FALSE)
v = which(option == key)
number = c(number,v)
}
average = c(average,mean(number))
}
mean(average)
4996.567
hist(average, freq = FALSE)
```

Stay tuned!

Stay Blessed!

Google