Lock and Key | ISI MStat 2017 PSB | Problem 6

Join Trial or Access Free Resources

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}{10001-i} = \frac{1}{10000}\)

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

#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)
 a graph of lock and key problem
Close to 4999.5

Stay tuned!

Stay Blessed!

More Posts
ISI M.Stat Entrance Success Story 2026

ISI M.Stat Entrance Success Story 2026

June 27, 2026

In 2026, the following Cheenta students have been successful for Indian Statistical Institute's M.Stat Entrance. They ranked within the first 50 in the entire country in these entrances. I.S.I. M.Stat Entrance

Read More
ISI B.Stat-B.Math and CMI BSc. Math Entrance Success Story 2026

ISI B.Stat-B.Math and CMI BSc. Math Entrance Success Story 2026

In 2026, the following Cheenta students have been successful for Indian Statistical Institute's B.Stat Entrance and Chennai Mathematical Institute's B.Sc. Math Entrance. They ranked within the first 200 in the entire country in these entrances. Most of these students attended the problem solving workshops regularly, which happen 5 days every week. CMI B.Sc. Math Entrance […]

Read More
8 Cheenta students cracked the Regional Math Olympiad 2025 

8 Cheenta students cracked the Regional Math Olympiad 2025 

December 26, 2025

In 2025, 8 students from Cheenta Academy cracked the prestigious Regional Math Olympiad. In this post, we will share some of their success stories and learning strategies. The Regional Mathematics Olympiad (RMO) and the Indian National Mathematics Olympiad (INMO) are two most important mathematics contests in India.These two contests are for the students who are […]

Read More
Cheenta Students Shine at IOQM 2025

Cheenta Students Shine at IOQM 2025

October 26, 2025

Cheenta Academy proudly celebrates the success of 27 current and former students who qualified for the Indian Olympiad Qualifier in Mathematics (IOQM) 2025, advancing to the next stage — RMO. This accomplishment highlights their perseverance and Cheenta’s ongoing mission to nurture mathematical excellence and research-oriented learning.

Read More

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 comments on “Lock and Key | ISI MStat 2017 PSB | Problem 6”

  1. It is written that N= U +1. But after that , it is written E(N) = E( U ) ? Isn't should be "E(N) = E( U ) +1"?

© 2010 - 2025, Cheenta Academy. All rights reserved.
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram