Probability that 4th Question on Test is First Question Graded Right if Grading Order is Random Too I came across this one question in my textbook:
For each question on a multiple-choice test with 5 questions, there are five possible answers, of which exactly one is correct. If a student selects answers at random, give the probability that the first question answered correctly is question 4.
And I was wondering what would happen if the teacher also graded the test in a random order.
The way I approached the problem was by looking at a permutation of the sequence {1,2,3,4,5}. There's then a function that maps 1,2,3,4,5 to their position in the permutation we can call f.
The probability that the teacher grades in the order of that permutation and that question #4 is the first question right is then:
0.8^(f(4)-1)*0.2/(5!) since we need every question that were graded before #4 to be wrong and then for #4 to be right. The 1/5! is the probability that that specific grading order was chosen.
We can group the permutation functions by how they map 4. So the set of functions that map 4 to 1 would be one group and the set of functions that map 4 to 5 would be another group. There are 4! elements in each set (4! arrangements of 5 objects with one object being fixed) so the total probability is then
the sum from n=1 to 5 of 0.8^(n-1)*1/25 which I got to be roughly 0.134.
I think my answer is right but when I tried to check it experimentally in Python (simulating these probability problems makes for some good coding problems which is nice), I got 0.0711.
Here's the code:
import random

def experi():
    li = [];
    
    index = random.sample(range(1, 6), 5);
    
    for i in range(1,6):
        li.append(random.randint(1,6));
    
    #check if 4th question is right if not, return 0
    if li[3] != 1:
        return 0;
        
    #li is a list of random integers from 1-5
    #for simplicity we assume that all questions have same answer, 1
    #generate random integer, that is question number we need to get right first
    #then we shuffle range(1:5) to get a random indexing
    #we then iterate through the shuffle and check if first index to be right is 4
    
    # goes through index if it hits 4 then nothing before was correct
    #if it hits a right answer before 4, then we return false
    for i in index:
        if i == 3:
            return 1;
        if li[i-1] == 1:
            return 0;
    
    return 0;

count = 0;
N = 100000;
for i in range(N):
    count += experi();

print(count/N);
        


I would really appreciate it if you guys could tell me whether it's my code or my math that's wrong (or both).
 A: Each question is equally likely to be the first to be marked correct, and the sum of these probabilities plus the probability of having no correct answers must equal $1$. Therefore, the desired probability given $n$ questions is: $$\frac{1-\left(\frac45\right)^n}{n}$$
Evaluating this at $n=5$ gives $$\frac{1-\left(\frac45\right)^5}{5}=\frac{2101}{15625}=0.134464$$
A: The answer to the first question is $$\left(\frac 45\right)^3\times \frac 15=.1024$$
For the second problem:  work recursively (on the number of questions). Let $p_n$ be the desired probability if there are $n$ questions other than the special question.
Of course $p_0=\frac 15$, since the teacher only grades the special question, which has a $\frac 15$ chance of being right.
For $p_1$ note that there are two paths to success.  Either the teacher starts with the special question or with the other.  It follows that $$p_1=\frac 12\times \frac 15+\frac 12\times \frac 45\times \frac 15=\frac 9{50}$$
More broadly, we have the recursion $$p_n=\frac 1{n+1}\times \frac 15+\frac {n}{n+1}\times \frac 45\times  p_{n-1}$$
Which gets us to $$p_4=.134464$$
Which matches your claimed result, though not the simulation.
Worth remarking:  In the first problem, the special question was the fourth one the teacher consults, which is bad luck.  If the teacher grades randomly, there is an excellent chance that the special question comes up earlier than fourth, and only a $.2$ chance that it comes up worse than fourth.  Thus you should expect the answer to the second problem to be greater than the answer to the first, leading me to doubt your simulator.
A: Alternative approach:
For $n \in \{1,2,3,4,5\}$, let $f(n)$ denote the probability that the $4$th question is graded in position $n$.
For $n \in \{1,2,3,4,5\}$, let $g(n)$ denote the probability that the $4$th question is the first question graded as correct, under the assumption that the $4$th question is in position $n$.
Then, the desired computation is
$$\sum_{n=1}^5 [ ~f(n) \times g(n) ~].$$

One way of approaching the computation of $f(n)$ is to note that in order for the $4$th question to be graded in position $n$, that it must be missed $(n-1)$ times, and then hit, on the $n$th time.
However, by reasons of symmetry, the $4$th question is equally likely to be graded in any of the positions.  Therefore, for all $n \in \{1,2,3,4,5\}$, you have that $f(n) = \dfrac{1}{5}.$
A way of intuiting this is to assume that there are $5$ pieces of paper, each with a distinct number on them, from the set $\{1,2,3,4,5\}$.  The teacher arbitrarily decides that he will draw one of the pieces of paper at random, and whatever number is on that piece of paper, that is the position that the $4$th question will be graded in.

Assuming that the $4$th question is in position $n$, then that question must be correct, and the preceding questions must be wrong.
The probability of this happening is
$$g(n) = \frac{1}{5} \times \left[\frac{4}{5}\right]^{n-1}.$$

Therefore,
$$\sum_{n=1}^5 \left[ ~f(n) \times g(n) ~\right]
= \sum_{n=1}^5 \left[ ~\frac{1}{5} \times \frac{1}{5} \times \left(\frac{4}{5}\right)^{n-1} ~\right]$$
$$= \left(\frac{1}{5}\right)^2 \times 
\left[ ~1 + \frac{4}{5} + \left(\frac{4}{5}\right)^2
+ \left(\frac{4}{5}\right)^3
+ \left(\frac{4}{5}\right)^4 ~\right]$$
$$= \frac{1}{25} \times \frac{1 - \left(\frac{4}{5}\right)^5}{1 - \frac{4}{5}}
= \frac{1}{25} \times \frac{\frac{3125-1024}{3125}}{ \frac{1}{5}} $$
$$= \frac{1}{5} \times \frac{2101}{3125} = \frac{2101}{15625} \approx 0.1345.$$
A: A more direct approach involve more calculation, but anyway
We try to find number of ways where we the 4th question is at position $i$ and the first one to win.
$$= \frac{4!}{5!}\cdot\frac{1}{5} + \frac{4!}{5!}\cdot\frac{1}{5}\cdot\left(\frac{4}{5}\right) + \frac{4!}{5!}\cdot\frac{1}{5}\cdot\left(\frac{4}{5}\right)^2 + \frac{4!}{5!}\cdot\frac{1}{5}\cdot\left(\frac{4}{5}\right)^3+\frac{4!}{5!}\cdot\frac{1}{5}\cdot\left(\frac{4}{5}\right)^4 \\
= \frac{4!}{5\cdot5!}\cdot\left((\tfrac{4}{5})^0+(\tfrac{4}{5})^1+(\tfrac{4}{5})^2+(\tfrac{4}{5})^3+(\tfrac{4}{5})^4\right) $$
This geometric progression can be simplified into the answer
$$\frac{1-(\frac{4}{5})^5}{5}$$
As for the code I think the problem is that when you get the permutation, you need to check according to that whether the 4th problem is the first 1 to be correct or not:
import random

def experi():
    li = [];
    
    index = random.sample(range(1, 6), 5);
    
    for i in range(1,6):
        li.append(random.randint(1,6)); // this is wrong use randint(1,5)

    
    for i in range(0, 5):
        if (index[i] == 4):
            return li[i]==1;
        else:
            if (li[i] ==1) :
                return 0;
    return 0;


count = 0;
N = 100000;
for i in range(N):
    count += experi();

print(count/N);
    

Still this seems incorrect as I get answer around 0.12 on multiple runs whereas other answers have shown answer to be 0.1345. I am not sure what might be the error here.
If someone can clarify it would be great.
Edit:
Thanks to @Daniel Mathias. The correction in code is that we need to write li.append(random.randint(1,5));  to get random integer from 1 to 5 inclusive
